diff options
author | evuez <helloevuez@gmail.com> | 2015-10-16 10:42:41 +0200 |
---|---|---|
committer | evuez <helloevuez@gmail.com> | 2015-10-16 10:42:41 +0200 |
commit | 85adff2c39e6eae77a915638961f2144ef18b5c9 (patch) | |
tree | ad87847bd5cca361b2e4582a361c4d4d70948aec /bash.html.markdown | |
parent | 375f0c18ae9b8d8732c480d0647becf1e82adf3e (diff) | |
parent | 25e49b8cf10d3879431eae6ffe8a8e5f88843b8d (diff) |
Merge 'master' of github.com:adambard/learnxinyminutes-docs
Diffstat (limited to 'bash.html.markdown')
-rw-r--r-- | bash.html.markdown | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/bash.html.markdown b/bash.html.markdown index 191f916a..211d2944 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -90,17 +90,26 @@ else echo "Your name is your username" fi +# NOTE: if $Name is empty, bash sees the above condition as: +if [ -ne $USER ] +# which is invalid syntax +# so the "safe" way to use potentially empty variables in bash is: +if [ "$Name" -ne $USER ] ... +# which, when $Name is empty, is seen by bash as: +if [ "" -ne $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 |