I am trying to understand a piece of code here and as I am not an expert on bash, I am struggling to find out what the below if does:   if [[ &quot;$4&quot; == *&quot;-a &quot;* ]]; then  I understand the $4 is the fourth parameter, but I am not getting the rest... Why the condition is between *? Thank you folks, appreciate any help. Regards, Thiago
13832
I have a string in Bash: string=&quot;My string&quot;  How can I test if it contains another string? if [ $string ?? 'foo' ]; then   echo &quot;It's there!&quot; fi  Where ?? is my unknown operator. Do I use echo and grep? if echo &quot;$string&quot; | grep 'foo'; then   echo &quot;It's there!&quot; fi  That looks a bit clumsy.
3953
I have the following if block in my bash script:  if [ ${PACKAGENAME} -eq kakadu-v6_4-00902C ]; then   echo "successfully entered if block!!" fi   The script execution is not entering my if block even though $PACKAGENAME is equal to kakadu-v6_4-00902C. What am I doing wrong?
3757
I have a script running on Linux that accepts some parameters. I would like to do something like:    if [[ $CONDITION == "true" ]]; then     script param1 --param2   else   script param1   fi    I would like to avoid the forking path of the if. Is there a more optimal way to pass the second parameter?
57835
says:  If the index number is @ or *, all members of an array are referenced.  When I do this: LIST=(1 2 3) for i in &quot;${LIST[@]}&quot;; do   echo &quot;example.$i&quot; done  it gives the desired result: example.1 example.2 example.3  But when I use ${LIST[*]}, I get example.1 2 3  instead. Why? Edit: when using printf, @ and * actually do give the same results.
115449
Stupidly, I had been using a condition like this as part of a script:  if [ $(ls FOO* 2&gt; /dev/null) ] # if files named "FOO*" were downloaded  then     echo "Files found"      # ... process and email results else     echo "Not found"     # ... email warning that no files were found (against expectations) fi   That works for zero and one files named FOO*, but fails if there are more than one. From logs I found several different error messages stemming from this:  [: FOO_20131107_082920: unary operator expected [: FOO_20131108_070203: binary operator expected [: too many arguments   My question is: what is the correct way to check, in a Bash if condition, whether one or more files whose name begins with FOO exist?  GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
91295
Why doesn't echo $1 print $1 in this simple bash script?    #!/bin/bash # function.sh print_something () { echo $1 } print_something  $ ./function.sh 123  -&gt; why doesn't it print '123' as a result?
48283
I am taking the Linux Essentials class and was doing well until I hit the scripting chapter. I simply do not understand these concepts.  Wondering if someone can break the following down into ultra simplistic terms or point me to a better reference to learn it. I'm currently using netacad's curriculum.        From the text book (with minor formatting changes):     There are some special variables in addition to the ones you set. You   can pass arguments to your script:  #!/bin/bash echo "Hello $1"       A dollar sign followed by a number N corresponds to the Nth argument   passed to the script. If you call the example above with ./test.sh the   output will be Hello Linux. The $0 variable contains the name of the   script itself.      After a program runs, be it a binary or a script, it returns an exit   code which is an integer between 0 and 255. You can test this through   the $? variable to see if the previous command completed successfully.     I understand how to assign variables and how they work with the $ but the whole issue with $0 and $1 -- I just don't get it.    Any help would be much appreciated.
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?
79418
What does $# mean in shell?  I have code such as  if [ $# -eq 0 ] then   I want to understand what $# means, but Google search is very bad for searching these kinds of things.
39889
This script:  a=2 [[ "$a" -eq 2 ]] &amp;&amp; echo yes1 [[  $a  -eq 2 ]] &amp;&amp; echo yes2 [[   a  -eq 2 ]] &amp;&amp; echo yes3 [[  "a" -eq 2 ]] &amp;&amp; echo yes4   Does not work in  (and some others that do not have [[ ). Fails the last two "yes tests" in  (BusyBox ash). As I expected.  But, quite UN expectly: This happens when the arithmetic  » -eq «  is being used (and some other). Prints all the four yes in , , , ,  and .  Q's  Is this the natural side effect of "Arithmetic Expansion" being applied to labels like (a) ?. (nothing to see, move along ?)  Should (a) still be expanded as a variable when quoted: "a"?  Is this documented somewhere I haven't look into yet?  Or, Is a simple (very old) bug?  Edit  To really get you thinking, check this:  a=2; b=2  [[ "$a" -eq "$b"                 ]] &amp;&amp; echo yes1 [[  $a  -eq "$b"                 ]] &amp;&amp; echo yes2 [[   a  -eq "$b"                 ]] &amp;&amp; echo yes3 [[  "a" -eq "$b"                 ]] &amp;&amp; echo yes4 [[ "$a" -eq "$b" &amp;&amp; "$a" == "$b" ]] &amp;&amp; echo new1 [[  $a  -eq "$b" &amp;&amp;  $a  == "$b" ]] &amp;&amp; echo new2 [[   a  -eq "$b" &amp;&amp;   a  == "$b" ]] &amp;&amp; echo new3 [[  "a" -eq "$b" &amp;&amp;  "a" == "$b" ]] &amp;&amp; echo new4   All yes work, only new1 and new2 do. The var a is not being expanded in new3 &amp; 4.  That means that shells switch mode (Arithmetic -- String) in the middle of the evaluation.  This doesn't seem so obvious to me. At least, is not simple, IMO.  Note that ash does it in a different way. And also report the error.  Conclusion:  I do not know the correct answer. But I suspect (looking at so many shells doing it exactly the same way) that it is a very well known way (to developers) in which this tests should work. It should be up to us to understand the fact and adapt.  Seems that for numeric -eq the only possible 'Conditional Expression' to apply is:  exp1 -eq exp2           true if exp1 is numerically equal to exp2.   Which makes both sides of the -eq "Arithmetic expressions"  While for the == the only possible 'Conditional Expression' to apply is:  string == pattern           true if string matches pattern. ....   Which makes the left hand side an string (no expansion?) and the right hand side a pattern (which really do not apply to the test I presented as all were quoted on the right side to avoid this complexity. So, right hand side defaults (being quoted) to an string also (no arithmetic expansion here).  But maybe I am more confused than I though :-)  What do you say?
115440
Simply said, I just can't find a way to match a variable against some pattern containing spaces.  Here is what I expected to work (echo 'ok' string)  item='foobar baz' pat=".+bar baz" if [[ "$item" =~ "$pat" ]] ; then     echo ok fi   Adding/removing quotes around $pat does not seem to make any difference. I get those two erros below:  bash: conditional binary operator expected bash: syntax error near `~='   Could someone please help me pointing out what I am doing wrong here ? Should I put the pattern right away (without any quotes/double quotes, nor variable reference) ? If that is the case, then how can I put spaces in ? (using reg-exp matching, not an alternative)  Thank you !
66729
I want to compare two floating point numbers in a shell script. The following code is not working:  #!/bin/bash    min=12.45 val=10.35     if (( $val &lt; $min )) ; then       min=$val fi echo $min
42820
When going through one shell script, I saw the term "$?". What is the significance of this term?
67538
How does "${2:-}" work in an 'if-then-else example' below?  Somehow I cannot help but think that being ${2:-} it means the second argument, but I am curious what the colon(:) and dash(-) after the digit 2 mean?    if [ "${2:-}" = "Y" ]; then      prompt="Y/n"      default=Y   elif [ "${2:-}" = "N" ]; then      prompt="y/N"      default=N   else      prompt="y/n"      default=   fi    read -p "$1 [$prompt] " REPLY &lt;/dev/tty
10330
The following code  number=1 if [[ $number =~ [0-9] ]] then   echo matched fi   works. If I try to use quotes in the regex, however, it stops:  number=1 if [[ $number =~ "[0-9]" ]] then   echo matched fi   I tried "\[0-9\]", too. What am I missing?  Funnily enough,  suggests this should work.  Bash version 3.2.39.
111870
I often use ($var &amp; 1) in my code, which returns true if $var is an odd number and false if it's an even number.  But what does "&amp;" actually do?
114683
# touch $$ # gzip $$ # gzip --test $$.gz # echo $? 0 # OUT=$(gzip --test $$.gz) # echo $OUT  # if [ -z $OUT ] ; then echo $$ ; fi 26521 # if [ -n $OUT ] ; then echo $$ ; fi 26521 #      from bash(1)      -z string           True if the length of string is zero.    string    -n string           True if the length of string is non-zero.    I'm confused, how is it zero and non-zero at the same time? How does one do check against if key has a value (using bash)?