diff options
Diffstat (limited to 'bash.html.markdown')
| -rw-r--r-- | bash.html.markdown | 15 | 
1 files changed, 12 insertions, 3 deletions
| diff --git a/bash.html.markdown b/bash.html.markdown index 191f916a..bd2d5984 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -83,24 +83,33 @@ echo Hello, $Name!  # We have the usual if structure:  # use 'man test' for more info about conditionals -if [ $Name -ne $USER ] +if [ $Name != $USER ]  then      echo "Your name isn't your username"  else      echo "Your name is your username"  fi +# NOTE: if $Name is empty, bash sees the above condition as: +if [ != $USER ] +# which is invalid syntax +# so the "safe" way to use potentially empty variables in bash is: +if [ "$Name" != $USER ] ... +# which, when $Name is empty, is seen by bash as: +if [ "" != $USER ] ... +# which works as expected +  # There is also conditional execution  echo "Always executed" || echo "Only executed if first command fails"  echo "Always executed" && echo "Only executed if first command does NOT fail"  # To use && and || with if statements, you need multiple pairs of square brackets: -if [ $Name == "Steve" ] && [ $Age -eq 15 ] +if [ "$Name" == "Steve" ] && [ "$Age" -eq 15 ]  then      echo "This will run if $Name is Steve AND $Age is 15."  fi -if [ $Name == "Daniya" ] || [ $Name == "Zach" ] +if [ "$Name" == "Daniya" ] || [ "$Name" == "Zach" ]  then      echo "This will run if $Name is Daniya OR Zach."  fi | 
