How to Pass Argument with Exclamation Mark on Linux

How to pass argument with exclamation mark on Linux?

You should be able to simply wrap things in single quotes in the shell.

$ emailsender.py -u username -p 'pass!!'

How do I escape an exclamation mark in bash?

Exclamation mark is preserved literally when you include it in a single-quoted string.

Example:

git commit -m 'Frustrating <insert object of frustration here>!'

Jupyter notebook exclamation mark arguments

This is possible using {}, such as:

extention='*.png'
!ls {extention}

Strange behavior of argv when passing string containing !!!!

This is not related to your code but to the shell that starts it.

In most shells, !! is shorthand for the last command that was run. When you use double quotes, the shell allows for history expansion (along with variable substitution, etc.) within the string, so when you put !! inside of a double-quoted string it substitutes the last command run.

What this means for your program is that all this happens before your program is executed, so there's not much the program can do except check if the string that is passed in is valid.

In contrast, when you use single quotes the shell does not do any substitutions and the string is passed to the program unmodified.

So you need to use single quotes to pass this string. Your users would need to know this if they don't want any substitution to happen. The alternative is to create a wrapper shell script that prompts the user for the string to pass in, then the script would subsequently call your program with the proper arguments.

Bash:Single Quotes and Double Quotes and Exclamation Mark


$ cat t.sh
#! /bin/bash
echo -e $@

Or echo -e $1, or echo -e ${1} if you just want to process the first argument.

To get bash to stop trying to expand !, use set +H (see In bash, how do I escape an exclamation mark?)

$ set +H
$ ./t.sh "#1. You're smart!\n#2. It's a difficult question!"
#1. You're smart!
#2. It's a difficult question!

exclamation mark to test variable is true or not in bash shell

tl;dr

[ ! $bar ] treats $bar as a string, and any nonempty string is considered "true" - including literal false; in other words: [ ! 'true' ] && [ ! 'false' ] both evaluate to "false", because the operands are nonempty strings in both cases and ! negates the outcome.

Therefore, you must use string comparison:

bar=false
if [ ! "$bar" = 'true' ]; then # same as: [ "$bar" != 'true' ]
echo 'bar is false'
else
echo 'bar is true'
fi

In Bash, you can also use [[ ! $bar == 'true' ]] or [[ $bar != 'true' ]]; unless you need to remain POSIX-compliant, I suggest using [[ ... ]].


In the context of [ ... ] and [[ ... ]] (Bash-specific), variable values are strings by default, and, generally, POSIX-like shells have no explicit Boolean data type.

Unary operator ! interprets its operand implicitly as a Boolean, and a string in a Boolean context is interpreted as follows:

  • only an empty string is considered "false" (exit code 1(!))
  • any nonempty string - including literal false - is considered "true" (exit code 0(!))

Thus, ! $bar evaluates to "false", because $bar - containing nonempty string 'false' - evaluates to "true", and ! inverts that.


! can also be used outside of conditionals to directly negate the success status of commands (including [).

Since false and true also exist as command names (typically, as shell builtins), you could do the following, but do note that it only works as intended with variable values that are either the literals false or true:

bar=false
if ! "$bar"; then
echo 'bar is false'
else
echo 'bar is true'
fi

Background information

POSIX-like shells only have 2 basic (scalar) data types:

  • strings (by default):

    • In [ ... ] and [[ ... ]] conditionals, operators = (== in Bash),<,<=,>,>=` perform string comparison.
  • integers:

    • In [ ... ] and [[ ... ]] conditionals, distinct arithmetic operators (-eq, lt, le, gt, ge) must be used to perform numerical comparison

    • Alternatively, in an arithmetic expansion ($(( ... ))), ==, <, <=, >, >= have their usual, numeric meaning.

      In Bash, you can also use (( ... )) as an arithmetic conditional.

Note: Per POSIX, you cannot type a variable as an integer (because there is no way to declare a shell variable), so it is only implicitly treated as one in comparisons using arithmetic operators / in arithmetic contexts.

In Bash, however, you can declare integer variables with declare -i / local -i, but in conditionals / arithmetic contexts it is still the choice of operator / arithmetic context that determines whether the value is treated as a string or as an integer.


Booleans in shells are expressed implicitly as exit codes, in a reversal of the usual Boolean-to-integer mapping:

  • "true" maps to exit code 0(!)
  • "false" is expressed as exit code 1(!) or any other nonzero exit code.

Pass a string with different punctuation symbols as argv from command line

Bash won't interpret strings inside single quotes so if you could switch:

"('a','a');('a','!a');"

for

'("a","a");("a","!a");'

the later with double quotes inside single quotes will work and you don't have to look at nasty escapes.

Perl: Can't pass question mark as command line argument

You have a file named t in the directory in which you are running your script. The shell treats a question mark (?) as a single character and expands it prior to passing the argument to your script. If you have a file named xx, for example, then running your script with ?? would print "xx".

If you quote your script's argument, no shell expansion occurs and passing ? or * or ?? remain uninterpreted.



Related Topics



Leave a reply



Submit