I thought    [ 1 -eq $1 ] &amp;&amp; echo "yes" || echo "no"   Acts like  if [ 1 -eq $1 ]; then     echo "yes" else     echo "no" fi   But, when I run this script (nocmd is a non-existing command)  #!/bin/bash  [ 1 -eq $1 ] &amp;&amp; nocmd "yes" || echo "no"   I get a strange output for parameter '1':  me@ubuntu:/tmp$ ./ddd.sh 0 no me@ubuntu:/tmp$ ./ddd.sh 1 ./sh.sh: line 3: nocmd: command not found no   Looks like it acts like:  if [ 1 -eq $1 ]; then     nocmd "yes"     if [ $? -ne 0 ]; then         echo "no"     fi else     echo "no" fi   Is it fine? Am I missing something?
56687
I am trying to understand how the logical operator precedence works in bash. For example, I would have expected, that the following command does not echo anything.  true || echo aaa &amp;&amp; echo bbb   However, contrary to my expectation, bbb gets printed.   Can somebody please explain, how can I make sense of compounded &amp;&amp; and || operators in bash?
9236
How can I do something like this in bash?  if "`command` returns any error"; then     echo "Returned an error" else     echo "Proceed..." fi
96788
I'm sure this is posted somewhere, but I haven't been able to find it.  In Bash, how does one specify operator precedence (aka command grouping) without creating a subshell?  In most other languages the() do this, but in Bash this runs the commands in a subshell which "discards" environment changes.  I want to specify operator precedence without losing environment changes.    Specifically, I'd like to do something like this and have the entire script exit, not just the subshell in the ():  die () {     echo "[DIE]: $1"     exit 1 }  # When installChruby returns an error, print the error message and exit [[ $CHRUBY =~ [Yy] ]] &amp;&amp; (installChruby || die "Error installing chruby")   I figured out a "workaround" by doing this, but it's not a pretty one-liner like I want:  if [[ $CHRUBY =~ [Yy] ]]; then installChruby || die "Error installing Chruby"; fi    The desired outcome is to do nothing and continue if CHRUBY is unset, to call the function installChruby if CHRUBY is either Y or y, and to call the die function only if the installChruby function returns false.   Is there an operator in Bash that does this besides (), or is there a way to tell the code inside the () to not run in a sub-shell?
62905
Look at this if block:  #!/bin/bash  str="m.m" if [[ "${str}" =~ "m\.m" ]]; then     echo "matched" else     echo "not matched"     exit 1 fi  exit 0   This should print "matched", but it doesn't. Where am I going wrong?
106163
I have an echo statement in my script as below:  echo -ne "Check Script";   I was expecting it to print   Check Script   but I am getting the below output  -ne Check Script   But when I run the same script on some other machine I get the expected output. What could be machine specific variables or properties because of which the script is behaving differently.
37655
In Bash, two integers can be compared using conditional expression     arg1 OP arg2      OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These   arithmetic binary operators return true if arg1 is equal to, not equal   to, less than, less than or equal to, greater than, or greater than or   equal to arg2, respectively. Arg1 and arg2 may be positive or negative   integers.   or arithmetic expression:     &lt;= &gt;= &lt; &gt; comparison      == != equality and inequality   Why do we have two different ways for comparing two integers? When to use which?  For example, [[ 3 -lt 2 ]] uses conditional expression, and (( 3 &lt; 2 )) uses arithmetic expression. Both returns 0 when the comparison is true  When comparing two integers, can these two methods always be used interchangeably? If yes, why does Bash have two methods rather than one?
104529
I have seen this construct in scripts a lot and used it myself, but it bothers me that I can't seem to find it in the documentation.  Example:  [ -f file1 ] &amp;&amp; [ -f file2 ] &amp;&amp; echo "Both files exist." || echo "One or the other file doesn't exist."   This could also be done with backslashes before the newlines, as mentioned in man bash:  If a \&lt;newline&gt; pair appears,  and  the  backslash  is  not itself  quoted,  the \&lt;newline&gt; is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).   Example:  [ -f file1 ] &amp;&amp; \ [ -f file2 ] &amp;&amp; \ echo "Both files exist." || \ echo "One or the other file doesn't exist."   ...but this doesn't seem to be necessary.  The first version above works even without the backslashes.  Where can I find this in man bash?  (Also, is this bash specific or POSIX compliant?)
83024
I have two arrays:  arrayA=(1 2 3) arrayB=(a b c)   and I want to print out one of them using a command line argument, i.e., without any if else.  I tried a few variations on the syntax with no success. I am wanting to do something like this:  ARG="$1"  echo ${array${ARG}[@]}   but I get a "bad substitution" error. How can I achieve this?
18187
Can somebody explain what is going on in this command:  echo foo &gt;&amp;2 | grep foo   Zsh (5.0.2) prints foo twice. One seemingly to stdout and one to stderr, since running these commands:  { echo foo &gt;&amp;2 | grep foo } &gt;/dev/null   and:  { echo foo &gt;&amp;2 | grep foo } 2&gt;/dev/null   both just print foo once.
49040
for i in $(xrandr); do echo  "$i" ; done for i in "$(xrandr)"; do echo "$i"; done for i in "$(xrandr)"; do echo $i; done   I understand why 1 differs from 2. But why does 3 give a different output from 2? Please explain the output too. How do quotes work on newlines?
23266
I just saw this in an init script: echo $&quot;Stopping Apache&quot;  What is that dollar-sign for? My research so far: I found this in the bash manual:  extquote  If set, $'string' and $&quot;string&quot; quoting is performed within ${parameter} expansions enclosed in double quotes.  This option is enabled by default.   ...but I'm not finding any difference between strings with and without the $ prefix: $ echo &quot;I am in $PWD&quot; I am in /var/shared/home/southworth/qed $ echo $&quot;I am in $PWD&quot; I am in /var/shared/home/southworth/qed $ echo $&quot;I am in ${PWD}&quot; I am in /var/shared/home/southworth/qed $ echo &quot;I am in ${PWD}&quot; I am in /var/shared/home/southworth/qed $ echo 'I am in ${PWD}' I am in ${PWD} $ echo $'I am in ${PWD}' I am in ${PWD} $ echo $'I am in $PWD' I am in $PWD
2926
How should I write the command variable to get the expected behavior?  #!/bin/bash  function f ( ) {     echo "$2" } command="f --option=\"One Two Three\" --another_option=\"Four Five Six\"" $command  f --option="One Two Three" --another_option="Four Five Six"   First calling is wrong, second - right  $&gt; ./test.sh  Two --another_option=Four Five Six
38974
I have the following code snippet  for num do    echo $num done   But I don't understand why it works! How come does Bash know to loop through my params $1, $2, $3, $... using my personal taste of "num" variable???
20273
As a primarily Java programmer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain why the first one works, but not the second or third?  #works if [ -n $1 ]; then echo "parameterized"; fi  #output: ./links: Zeile 1: [-n: Kommando nicht gefunden. if [-n $1 ]; then echo "parameterized"; fi  #output: ./links: Zeile 1: [: Fehlende `]' if [ -n $1]; then echo "parameterized"; fi
25914
So I like to harden my bash scripts wherever I can (and when not able to delegate to a language like Python/Ruby) to ensure errors do not go uncaught.  In that vein I have a strict.sh, which contains things like:  set -e set -u set -o pipefail   And source it in other scripts. However, while pipefail would pick up:  false | echo it kept going | true  It will not pick up:  echo The output is '`false; echo something else`'    The output would be   The output is ''  False returns non-zero status, and no-stdout. In a pipe it would have failed, but here the error isn't caught. When this is actually a calculation stored in a variable for later, and the value is set to blank, this may then cause later problems.  So - is there a way to get bash to treat a non-zero returncode inside a backtick as reason enough to exit?
55413
The question is pretty straight forward. I can get the exit code of the previous command by using $?. But what exactly is it?  I don't think it is a variable, because you can't have any special characters in the name of variable other than _.  foo?foo=10   will result in the error: foo?foo=10: command not found  So if it is not a variable, what is it? Are there others like it?
2601
I want to pause input in a shell script, and prompt the user for choices. The standard Yes, No, or Cancel type question. How do I accomplish this in a typical bash prompt?