Skip to main content

Command Palette

Search for a command to run...

Bash shell finds out if a variable has a NULL value OR not

Published
1 min read
B
Senior Software Engineer | Backend, Cloud, Data, Machine Learning

In the Bash shell, you can use the -z option to check if a variable has a null (empty) value or not. Here's an example:

if [ -z "$my_var" ]; then
  echo "my_var is null or empty"
else
  echo "my_var is not null or empty"
fi

In the above example, $my_var is the variable that you want to check for null or empty value. The -z option checks if the length of the string in $my_var is zero, which means that it is null or empty.

Alternatively, you can use the -n option to check if a variable has a non-null value. Here's an example:

if [ -n "$my_var" ]; then
  echo "my_var has a value"
else
  echo "my_var is null or empty"
fi

In the above example, the -n option checks if the length of the string in $my_var is non-zero, which means that it has a value.

More from this blog

yesicbap

437 posts