...
Code Block | ||
---|---|---|
| ||
# Script version global variable. Edit this whenever changes are made. __ADVANCED_BASH_VERSION__="step_01" # Later, the value of this environment variable is referencesreferenced # by prefixing the name with the dollar sign ( $ ) echo "Current script version is: $__ADVANCED_BASH_VERSION__" # or enclosed in ${ } echo "Current script version is: ${__ADVANCED_BASH_VERSION__}" |
...
When defining or evaluating environment variables there's also a difference between enclosing the value in double quotes ( "$foo" ) or single quotes ( '$foo' ) – see Intro Unix: Quoting in the shell (Intro Unix).
Example:
Code Block | ||
---|---|---|
| ||
myvar="some text" echo "$myvar" echo '$myvar' |
...
And in bash, arguments passed to both scripts and functions are not enclosed in parentheses, as is the case in most many programming languages.
Example:
...
Since there is no formal argument list, it is good practice to copy function arguments into local variables with names suggesting their role. (e.g. local txt1=$1). The local keyword specifies that the variable scope is only within the function body – it is not visible to the caller or to called functions.
...
Dispatching to the appropriate function is done using a case/esac block. The case argument string ("$CMD$CMD") is matched against each clause using the clause text before the right parenthesis ")". The double-semicolon ";;" terminates each case clause, including the default case "*".
...
Code Block | ||
---|---|---|
| ||
function myfn() { echo "arg 1: $1"; echo "arg 2: $2"; echo "all args: $@"; } myfn foo bar baz wonk myfn foo "bar baz" wonk myfn "foo bar" baz wonk |
As described in Intro Unix: Quoting in the shell, this is the main function of both single and double quotes – to group text containing whitespace into one item.
Running the step_01.sh script
...