diff options
author | Luis Custodio <luis.custodio@gmail.com> | 2015-10-17 13:11:25 +0200 |
---|---|---|
committer | Luis Custodio <luis.custodio@gmail.com> | 2015-10-17 13:11:25 +0200 |
commit | f5873b08901bfaec3fb46518258ff9e886fd04c4 (patch) | |
tree | 62886b71c7226f0c6c4f979d61ff96c839a641a9 /bash.html.markdown | |
parent | c9348e5a82b639093f8f3eee955ffdf6fb99b5d8 (diff) | |
parent | 0e6d9f6fe9aeffc64c3adad3e4a0ee1cc0d1dd88 (diff) |
Merge branch 'master' of github.com:adambard/learnxinyminutes-docs
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 d4f3d424..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 @@ -252,7 +261,7 @@ grep "^foo.*bar$" file.txt grep -c "^foo.*bar$" file.txt # if you literally want to search for the string, # and not the regex, use fgrep (or grep -F) -fgrep "^foo.*bar$" file.txt +fgrep "^foo.*bar$" file.txt # Read Bash shell builtins documentation with the bash 'help' builtin: |