Is there any advantage/disadvantage of initializing the value of a bash variable in the script, either before the main code, or local variables in a function before assigning the actual value to it?

Do I need to do something like this:


init "Mark"

Is there any risk of variables being initialized with garbage values (if not initialized) and that having a negative effect of the values of the variables?

  • 2
    Where did you get this idea?
    – user103635
    Aug 22, 2018 at 8:23
  • 6
    @DoritoStyle Well, if one is used to lower level languages, such as C, then this is a perfectly valid thing to be concerned about.
    – Kusalananda
    Aug 22, 2018 at 8:26
  • @Kusalananda Isn't the C equivalent of that code name = ""; name = argv[1];? And isn't that just as pointless? Mar 29, 2020 at 20:06
  • 1
    @JosephSible-ReinstateMonica Yes. The C code that you posted is pointless. In C, though, as opposed to in the shell, uninitialized variables don't have a particular well defined value. This means that initializing variables in languages like C makes sense under many circumstances. Doing so in the shell is not needed. Initializing a variable in C only to immediately set it to another value is pointless, as you point out.
    – Kusalananda
    Mar 29, 2020 at 20:35

2 Answers 2


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).

  • 3
    While there is no value to setting a variable to an empty value and then immediately setting it as the OP literally does, there is value in setting an empty value (or unsetting) prior to running some kind of for or while loop to set it to a calculated value if there is any chance that loop will not actually run due to the condition(s) not being met; and the variable might have been set to some other value in the environment the script inherited. But it's arguably better to put everything in a main() and define the variables as local. Aug 21, 2018 at 17:52
  • 1
    You can also detect unset variables using the parameter expansion operators, by omitting the :, e.g. ${myvariable-defaultvalue}
    – Barmar
    Aug 22, 2018 at 19:29

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:

    local name
    echo "$name"

If I want to make it into a script, I just ignore the local statement and copy everything else:

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:

    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.

  • 1
    The question wasn't about combining the declaration and initialization. It was about whether to initialize with an empty value before assigning a real value.
    – Barmar
    Aug 22, 2018 at 19:26
  • @Barmar Oh, I just looked up "initialization". I thought it was synonymous with "declaration"...
    – wjandrea
    Aug 22, 2018 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.