3

Can someone tell me what is the meaning of a='' in a bash script?

For example:

 #!/bin/bash

    a=''
    case $@ in
    testfile)
             for list in $(cat $1);do

useradd -s /bin/false $list
         done
         ;;
$a)
         echo "empty"
         ;;
*)
         echo "wrong character"
         ;;
esac

Thank you

  • The question was about the meaning of the line with a=' ' exclusively. I still added the rest of the script for clarification but the answer has already been provided by Jesse_b and ilkkachu – qwerty1805 Feb 4 '18 at 11:01
6

This is a variable assignment. In this case a is your variable and you are setting it to a null value; '' will evaluate to nothing.

As ilkkachu points out:

Using a='' is effectively no different than using a="" or a=

As cas points out:

Setting the variable to a null value (or any initial/default value) also ensures you're not going to use whatever value it might have if it happens to be an environment variable (e.g. exported in the parent shell). This can be problem if, e.g., your script assumes that the variable is empty/undefined unless the script itself defines it, and takes different actions based on that.

script.sh

#!/bin/bash

if [[ $1 == null ]]; then
    a=
elif [[ $1 == unset ]]; then
    unset a
fi

echo "${a:-test}"

In action:

$ export a=value
$ ./script.sh null
test
$ ./script.sh unset
test
$ ./script.sh
value
$ echo $a
value

In many cases setting the variable to a null value is the same as unsetting the variable: unset a

However there are caveats

at least one

  • 3
    Unset is different than set to a null value, but this rarely matters. IFS is the only example I can think of where it does. – jordanm Feb 2 '18 at 22:48
  • 2
    also, a= is exactly equivalent to a='', or to a="". It just depends on if you want to put quotes there to clarify that you meant an empty value instead of, well, having forgotten to write a value there. – ilkkachu Feb 3 '18 at 0:16
  • 1
    +1. Setting the variable to a null value (or any initial/default value) also ensures you're not going to use whatever value it might have if it happens to be an environment variable (e.g. exported in the parent shell). This can be problem if, e.g., your script assumes that the variable is empty/undefined unless the script itself defines it, and takes different actions based on that. – cas Feb 3 '18 at 0:38
  • 1
    go right ahead. that's common practice here. comments are often given to help improve a good answer....and comments are ephemeral, they can be deleted without trace at any time if not incorporated into an answer. – cas Feb 3 '18 at 1:25
  • 1
    btw, my comment also applies to unsetting the variable...anything that changes it from whatever value it might have from the parent environment. Another alternative is to declare it as a local variable in the script or function. – cas Feb 3 '18 at 1:28

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.