diff options
author | Kristin Linn <klinn@upenn.edu> | 2015-10-20 16:26:35 -0400 |
---|---|---|
committer | Kristin Linn <klinn@upenn.edu> | 2015-10-20 16:26:35 -0400 |
commit | 396e6f5d9708f827512c4699240f72477366ff76 (patch) | |
tree | d63b41a4d91ea80c594574c48fc6416d6fd9a538 /bash.html.markdown | |
parent | 11aab085d656b79482e92a05acbbac81125bfb78 (diff) | |
parent | 5fb5dd7c7fd7670faca6b8cfff9ef1ffdbd65c0d (diff) |
Merge branch 'master' of https://github.com/adambard/learnxinyminutes-docs
Diffstat (limited to 'bash.html.markdown')
-rw-r--r-- | bash.html.markdown | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/bash.html.markdown b/bash.html.markdown index 08182c2c..211d2944 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -51,7 +51,7 @@ echo $Variable echo "$Variable" echo '$Variable' # When you use the variable itself — assign it, export it, or else — you write -# its name without $. If you want to use variable's value, you should use $. +# its name without $. If you want to use the variable's value, you should use $. # Note that ' (single quote) won't expand the variables! # String substitution in variables @@ -70,11 +70,11 @@ echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} # Builtin variables: # There are some useful builtin variables, like -echo "Last program return value: $?" +echo "Last program's return value: $?" echo "Script's PID: $$" -echo "Number of arguments: $#" -echo "Scripts arguments: $@" -echo "Scripts arguments separated in different variables: $1 $2..." +echo "Number of arguments passed to script: $#" +echo "All arguments passed to script: $@" +echo "Script's arguments separated into different variables: $1 $2..." # Reading a value from input: echo "What's your name?" @@ -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 @@ -108,8 +117,8 @@ fi # Expressions are denoted with the following format: echo $(( 10 + 5 )) -# Unlike other programming languages, bash is a shell — so it works in a context -# of current directory. You can list files and directories in the current +# Unlike other programming languages, bash is a shell so it works in the context +# of a current directory. You can list files and directories in the current # directory with the ls command: ls @@ -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: |