How to check if a Bash variable is set or not
There is no direct method in bash to determine if a variable is set or not. But we can use “parameter expansion” feature provided by bash. The following example script determines if bash variables MYVAR and MYVAR1 are set or not.
#!/bin/bash
MYVAR=hello
if [ -n "${MYVAR+x}" ]; then
echo MYVAR is set
else
echo MYVAR is not set
fi
if [ -n "${MYVAR1+x}" ]; then
echo MYVAR1 is set
else
echo MYVAR1 is not set
fi
Here is the output of above script where MYVAR is set but MYVAR1 is not set.
[neo@techpulp ~]# sh vset.sh MYVAR is set MYVAR1 is not set [neo@techpulp ~]#
However there are multiple ways to determine if a variable is set but contains blank or not.
Method 1:
The following condition determines if a variable contains null string or blank.
if [ "$MYVAR" == "" ]; then echo MYVAR is empty else echo MYVAR is not empty fi
Method 2:
case $MYVAR in '') echo MYVAR is not set;; *) echo MYVAR is set;; esac
Method 3:
if [ ! -n "$MYVAR" ]; then echo MYVAR is blank else echo MYVAR is not blank fi
All the above methods can be applied to check if an environment variable is set or not.


about 2 years ago
>> There is no direct method in bash to determine if a variable is set or not.
Really?
See snippet from the bash website below …
${parameter=default}, ${parameter:=default}
If parameter not set, set it to default.
Both forms nearly equivalent. The : makes a difference only when $parameter has been declared and is null, [1] as above.
about 1 year ago
I guess the focus is not about assigning default value.
The following lists possible modifiers:
Modifiers can be applied to the ${name} form of parameter substitution:
${name:-word}
if name is set and not null, it is substituted, otherwise word is substituted.
${name:+word}
if name is set and not null, word is substituted, otherwise nothing is substituted.
${name:=word}
if name is set and not null, it is substituted, otherwise it is assigned word and the resulting value of name is substituted.
${name:?word}
if name is set and not null, it is substituted, otherwise word is printed on standard error (preceded by name:) and an error occurs (normally causing termination of a shell script, function or .-script). If word is omitted the string ?parameter null or not set? is used instead.
In the above modifiers, the : can be omitted, in which case the conditions only depend on name being set (as opposed to set and not null).
If word is needed, parameter, command, arithmetic and tilde substitution are performed on it; if word is not needed, it is not evaluated.
about 1 year ago
Various comparisons that can be used:
[ -n "${VAR+x}" ] # Fails if VAR is unset
[ -n "${VAR:+x}" ] # Fails if VAR is unset or empty
[ -n "${VAR-x}" ] # Succeeds if VAR is unset
[ -n "${VAR:-x}" ] # Succeeds if VAR is unset or empty
about 1 year ago
Similar logic can be used to test if a particular environment variable is set or not. All environment variables are directly accessible in a bash script as variables. For example, if you need value of HOME environment variable, you can directly refer $HOME in the bash shell script.
about 1 year ago
The following appears function helps in determining if a variable is set or not.
isset()
{
[ ${!1-X} == ${!1-Y} ]
}
[sara@mypc ~]# isset MYVAR || echo notset
notset
[sara@mypc ~]# MYVAR=X
[sara@mypc ~]# isset MYVAR || echo notset
[sara@mypc ~]# MYVAR=
[sara@mypc ~]# isset MYVAR || echo notset
[sara@mypc ~]# unset MYVAR
[sara@mypc ~]# isset MYVAR || echo notset
notset
[sara@mypc ~]# isset BASH_VERSION[2] || echo notset
[sara@mypc ~]# isset BASH_VERSION[20] || echo notset
notset
[sara@mypc ~]#
about 1 year ago
Can some one explain how we can do it using “sed” command
about 1 year ago
[liz@techpulp ~]$ echo “Hello, Baby” | sed -e ‘s/./\U&\E/g’
HELLO, BABY
[liz@techpulp ~]$
about 1 year ago
If you like to know how it can be done using “awk” command, try following:
[liz@techpulp ~]# echo “Hello, Baby” | awk ‘{print toupper($0);}’
HELLO, BABY
[liz@techpulp ~]# echo “Hello, Baby” | awk ‘{print tolower($0);}’
hello, baby
[liz@techpulp ~]#
about 9 months ago
Hi all,
is there a way to use
if [ -n "${MYVAR+x}" ];
with the name of MYVAR stored in another var?
Something with ${!…}, but I cannot make it work.
Thanks.
about 4 months ago
This does not work when used in combination with the very useful switch -u
That is because if you try to do a variable expansion on an unset variable, you will get trapped by the -u, because you are precisely trying to use the variable before setting it.
I’ve a workaround for my case, as I wanted to test that for parameters, and I can simply use the parameter count $# instead, but I still haven’t found a solution compliant with -u