Initializing Bash variables - Is it required, recommended or define as you go
There is no benefit to assigning an empty string to a variable and then immediately assigning another variable string to it. An assignment of a value to a shell variable will completely overwrite its previous value.
There is to my knowledge no recommendation that says that you should explicitly initialize variables to empty strings. In fact, doing so may mask errors under some circumstances (errors that would otherwise be apparent if running under set -u
, see below).
An unset variable, being unused since the start of a script or explicitly unset by running the unset
command on it, will have no value. The value of such a variable will be nothing. If used as "$myvariable"
, you will get the equivalent of ""
, and you would never get "garbage data".
If the shell option nounset
is set with either set -o nounset
or set -u
, then referencing an unset variable will cause the shell to produce an error (and a non-interactive shell would terminate):
$ set -u
$ echo "$myvariable"
/bin/sh: myvariable: parameter not set
or, in bash
:
$ set -u
$ echo "$myvariable"
bash: myvariable: unbound variable
Shell variables will be initialized by the environment if the name of the variable corresponds to an existing environment variable.
If you expect that you are using a variable that may be initialized by the environment in this way (and if it's unwanted), then you may explicitly unset it before the main part of your script:
unset myvariable # unset so that it doesn't inherit a value from the environment
which would also remove it as an environment variable, or, you may simply ignore its initial value and just overwrite it with an assignment (which would make the environment variable change value too).
You would never encounter uninitialized garbage in a shell variable (unless, as stated, that garbage already existed in an environment variable by the same name).
Edit: Whoops, apparently declaration is different from initialization. I'll leave this here anyway so novice coders like myself can learn from my mistake.
The advantage of declaring local variables in a function is that you can easily copy the code.
For example, say I have a function:
foo(){
local name
name="$1"
echo "$name"
}
If I want to make it into a script, I just ignore the local
statement and copy everything else:
#!/bin/bash
name="$1"
echo "$name"
If the declaration and assignment were in the same line, I'd have to manually edit out the local
part before I could turn it into a script:
foo(){
local name="$1"
echo "$name"
}
In this example it's not a big deal, but if you're dealing with bigger, more complex functions, it can get more painful.