I want to set a variable if my condition is true on my Ubuntu system.  This proves that my if-statement is correct:  $ (if [ 1 == 1 ]; then echo "hi there"; fi); hi there   This proves that I can set variables:  $ a=1 $ echo $a 1   This shows that setting a variable in the if-statement DOES NOT work:  $ (if [ 1 == 1 ]; then a=2; fi); $ echo $a 1   Any ideas why? All my  indicates that it should work like this...
123139
While reading up on how to set up grub, I came across an article claiming that I need to use one of the following two syntaxes,  echo \(hd0,0\) &gt;&gt; /boot/grub/grub.conf   or  echo '(hd0,0)' &gt;&gt; /boot/grub/grub.conf   because, at the command line, parentheses are interpreted in a special way. What is special about the parentheses? How are they interpreted?
69637
i am writing a script in bash and I would like to give the two numbers in the brackets, x and y to be precise, as variables.  I wrote the script this way:  echo "x?" read x echo "y" read y  for i in {$x..$y}   but it does not work.  How could I set it up to make it function?  Thanks a lot in advance.
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?
42231
One of my coworkers has provided me with a Bash syntax that I am unfamiliar with.  My Google foo has failed me on figuring out what it does and why/when I should use it.    The command that he sent me was of this form:  someVariable=something command   Initially, I thought that this was equivalent to the following:   someVariable=something ; command   Or  someVariable=something  command   But this doesn't appear to the be case.  Examples:   [Jan-03 11:26][~]$ # Look at the environment variable BAZ. It is currently empty [Jan-03 11:26][~]$ echo $BAZ  [Jan-03 11:27][~]$ # Try running a command of the same format     [Jan-03 11:27][~]$ BAZ=jake echo $BAZ  [Jan-03 11:27][~]$     [Jan-03 11:27][~]$ # Now, echo BAZ again. It is still empty:     [Jan-03 11:27][~]$ echo $BAZ  [Jan-03 11:27][~]$     [Jan-03 11:28][~]$     [Jan-03 11:28][~]$ # If we add a semi-colon to the command, we get dramatically different results: [Jan-03 11:28][~]$ BAZ=jake ; echo $BAZ jake [Jan-03 11:28][~]$ [Jan-03 11:28][~]$ # And we can see that the variable is actually set: [Jan-03 11:29][~]$ echo $BAZ jake [Jan-03 11:29][~]$   What does this syntax do?  What happens to the variable that has been set?  Why does this work?
1646
The following script does not behave as I would have expected.  Adding spaces around the '=' in the conditional made it perform how I wanted, but it got me thinking, what is it actually doing inside the conditional?  #!/bin/bash S1='foo' S2='bar' if [ $S1=$S2 ]; then     echo "S1('$S1') is equal to S2('$S2') fi echo $S1 echo $S2   The output is:  S1('foo') is equal to S2('bar') foo bar   The contents of S1 and S2 don't change from what they are assigned, so the = doesn't perform an assignment.
23088
I'm confused with using single or double brackets. Look at this code:  dir="/home/mazimi/VirtualBox VMs"  if [[ -d ${dir} ]]; then     echo "yep" fi   It works perfectly although the string contains a space. But when I change it to single bracket:  dir="/home/mazimi/VirtualBox VMs"  if [ -d ${dir} ]; then     echo "yep" fi   It says:  ./script.sh: line 5: [: /home/mazimi/VirtualBox: binary operator expected   When I change it to:  dir="/home/mazimi/VirtualBox VMs"  if [ -d "${dir}" ]; then     echo "yep" fi   It works fine. Can someone explain what is happening? When should I assign double quotes around variables like "${var}" to prevent problems caused by spaces?
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)
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
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?
1260
I am trying to print a simple value for $AR1_p1 but the variable $i is not evaluating. for i in 1 2 3 4 do     AR1_p1=22     AR1_p2=23     AR1_p3=24     AR1_p3=25     echo $AR1_p$i done   It's like concatenating dynamically. Any suggestions on how to fix this?.
107888
I'm new to shell scripting and I came across these expressions   ${var:-val} and ${var-val}  ${var:+val} and ${var+val}  ${var:=val} and ${var=val}  so how are they evaluated and what are the differences between them
24885
Why doesn't this command output "1"?  echo 1 | echo   I imagine it working this way:  1. echo 1 (outputs 1) 2. | echo (takes the 1 as an input, then echos it)   Isn't this what should happen?
110561
I'm new with shell programming. My goal is to give the user a selection which file should be run. I catch all available scripts in one var with:  var=$(find script*)   so far so good, this is the output:      echo $var     script1 script2 script3 scr...   now I want to store each file in a singel variable to run it in later selection  select SEL in script1 script2 script3 .......... case $SEL in         $Script1) echo ($Script1 will start) &amp;&amp; ./$script1 ;;         $Script2) echo ($Script2 will start) &amp;&amp; ./$script2 ;;         $Script3) echo ($Script3 will start) &amp;&amp; ./$script3 ;;         $Sc..) echo ($Scr.... will start) &amp;&amp; ./$scrip.... ;;         $Sc...) echo ($Sc...... will start) &amp;&amp; ./$scrip..... ;;         *) echo "choose on of the availabel files";;     esac done   I don't have a clue how should I should separate all files if I don't know how many and the exactly length of each file.  I tried to split with echo ${var[1]} and echo $var[1] but this dosn't work. I recive either a empty output or the whole string.
3828
This is irritating me. I seen several suggestions (all using different files and syntax) and none of them worked.  How do I set an environment variable for a specific user? I am on debian squeeze. What is the exact syntax I should put in the file to make ABC = "123"?
49713
I'm trying to create an if else statement to verify that the user has entered something. If they have it should run through the commands, and if not i want to echo a help statement.
111834
I am trying to use a variable consisting of different strings separated with a | as a case statement test. For example:  string="\"foo\"|\"bar\"" read choice case $choice in     $string)         echo "You chose $choice";;     *)         echo "Bad choice!";; esac   I want to be able to type foo or bar and execute the first part of the case statement. However, both foo and bar take me to the second:  $ foo.sh foo Bad choice! $ foo.sh bar Bad choice!   Using "$string" instead of $string makes no difference. Neither does using string="foo|bar".   I know I can do it this way:  case $choice in     "foo"|"bar")         echo "You chose $choice";;     *)         echo "Bad choice!";; esac   I can think of various workarounds but I would like to know if it's possible to use a variable as a case condition in bash. Is it possible and, if so, how?
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.