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 – ion.sash295 Feb 04 '18 at 11:01

1 Answers1

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

jesse_b
  • 37,005
  • 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 02 '18 at 22:48
  • @jordanm I think it makes a difference in sh tests as well. – jesse_b Feb 02 '18 at 22:53
  • Unsetting values of certain variables can affect shell behavior. See https://unix.stackexchange.com/a/400715/85039 for example. But as far as string comparison goes, they can be considered equivalent: https://unix.stackexchange.com/a/381465/85039 – Sergiy Kolodyazhnyy Feb 02 '18 at 23:08
  • @jordanm, ${par+blah} is the other one. – ilkkachu Feb 03 '18 at 00:15
  • 3
    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 03 '18 at 00: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 03 '18 at 00:38
  • @ilkkachu do you mind if I add your comments to the question? – jesse_b Feb 03 '18 at 00:59
  • 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 03 '18 at 01: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 03 '18 at 01:28
  • Do you want "case $@ in" or "case $1 in"? – D McKeon Feb 04 '18 at 19:09
  • In the script you posted, do you want: "case $@ in testfile) ... esac;" or "case $1 in testfile) ... esac;" ? – D McKeon Feb 05 '18 at 19:36
  • Facepalm! Yes, I thought I was replying to OP re the original script. There, the use of case $@ seemed unusual. – D McKeon Feb 05 '18 at 22:47