Bash Parameters and Variables: Difference between revisions
Line 69: | Line 69: | ||
declare -p | grep "declare .. ${var_name}" | sed -e 's/.*=//' | declare -p | grep "declare .. ${var_name}" | sed -e 's/.*=//' | ||
</pre> | </pre> | ||
=Booleans= | |||
Recommended usage pattern: | |||
Declaration: | |||
<pre> | |||
some_var=true | |||
</pre> | |||
Usage: | |||
<pre> | |||
${some_var} && echo "this will execute" | |||
if ${some_var}; then | |||
echo "this will execute" | |||
fi | |||
</pre> | |||
Do not use | |||
<pre> | |||
if [ ${some_var} ]; then ... | |||
</pre> | |||
This will always evaluate to "true" regardless of the some_var value. |
Revision as of 16:54, 24 May 2017
Internal
Global Variable
Global variables are only maintained within the context of the current shell. Once the shell exits, the global variables are discarded.
The global variables declared in a shell are visible inside the functions executed within that shell and inside functions invoked from function invoked from the shell, recursively.
The value of a global variable is not available to sub-shells, unless the variable is exported.
export
export VAR=VALUE
Declaring a global variable to be exported causes the variable and its value to be copied in the environment of a invoked sub-shell. The sub-shell gets a copy of the variable, not a reference. Modifying the variable from the sub-shell does not reflect into the value of the global variable maintained by the invoking shell.
Variables exported by Sub-Shells
If a sub-shell invoked from another shell exports a global variable, once the sub-shell exits, the exported variables are discarded, and the invoking shell does not sees them.
Local Variable
If a variable is declared inside a function, without any qualifier, it automatically becomes a global variable, and it is visible to the entire shell after the function execution, even after the function exits.
In order to prevent a variable declared inside of a function to become global, it must be declared as local: the local keyword designates a local variable. Local variables can only be declared inside a function.
Example:
local a=b
Local Variable Assignment and Failure
Do not do a local variable assignment from a function that may return non-zero and expect to use the return value to exit
Do not do something like this:
local a=$(do_something) || exit 1 # hoping to exit if 'do_something' returns a non-zero value
Even if do_something returns a non-zero error code, that is not detected and exit is not executed.
Do this instead:
local a; a=$(do_something) || exit
Listing Declared Variables
declare -p
A way of obtaining the value for a specific variable is:
local var_name="HISTSIZE" declare -p | grep "declare .. ${var_name}" | sed -e 's/.*=//'
Booleans
Recommended usage pattern:
Declaration:
some_var=true
Usage:
${some_var} && echo "this will execute" if ${some_var}; then echo "this will execute" fi
Do not use
if [ ${some_var} ]; then ...
This will always evaluate to "true" regardless of the some_var value.