What does -z mean in Bash? [duplicate] - bash

This question already has answers here:
Is there a list of 'if' switches anywhere?
(5 answers)
Closed 5 years ago.
I'm looking at the following code:
if [ -z $2 ]; then
echo "usage: ...
(The 3 dots are irrelevant usage details.)
Maybe I'm googling it wrong, but I couldn't find an explanation for the -z option.

-z string: True if the string is null (an empty string)
See https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions

-z
string is null, that is, has zero length
String='' # Zero-length ("null") string variable.
if [ -z "$String" ]
then
echo "\$String is null."
else
echo "\$String is NOT null."
fi # $String is null.

test -z returns true if the parameter is empty (see man sh or man test).

The expression -z string is true if the length of string is zero.

Related

How to check if array element is substring of string in Bash [duplicate]

This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 2 years ago.
This question does not answer my question.
I have a script that checks the existence of specific environment variables and prints them with their values. Now I want to mask the values of password variables (containing SECRET, PW, PASSWORD, KEY in its name, e.g. CLIENT_SECRET) with ****.
Currently I have a script like this:
expected_env_vars=("CLIENT_ID" "CLIENT_SECRET" "BACKEND_KEY" "BACKEND_NAME")
suppress_env_vars_with_substring=("SECRET" "PASSWORD" "PW" "KEY")
for env_var in "${expected_env_vars[#]}"; do
if [[ -z "${!env_var}" ]]; then
echo "Environment variable \"$env_var\" not defined"
exit 1
else
# Perform check if an element of $suppress_env_vars_with_substring is substring of $env_var
echo "$env_var=${!env_var}...OK"
fi
done
Question
How to check if an array element is substring of a string?
You may use this script with a grep:
expected_env_vars=("CLIENT_ID" "CLIENT_SECRET" "BACKEND_KEY" "BACKEND_NAME")
suppress_env_vars_with_substring=("SECRET" "PASSWORD" "PW" "KEY")
for env_var in "${expected_env_vars[#]}"; do
if [[ -z "${!env_var}" ]]; then
echo "Environment variable \"$env_var\" not defined"
exit 1
else
# Perform check if an element of $suppress_env_vars_with_substring is substring of $env_var
printf '%s=' "$env_var"
grep -qFf <(printf '%s\n' "${suppress_env_vars_with_substring[#]}") <<< "$env_var" &&
echo '****' || echo "${!env_var}"
fi
done
It does not matter, whether or not the operands to be tested belongs to an array or not. The general form is
[[ $x == *$y* ]] && echo "$y is a substring of $x"
You can substitute for $x and $y and parameter expansion you like, including array elements.

Shell always evaluates both factors of &&? [duplicate]

This question already has answers here:
Why does 'test -n' return 'true' in bash?
(2 answers)
Closed 2 years ago.
When $thing is null this quits...
thing=`command_that_could_return_null`
echo "thing is" $thing
if [ -z $thing ]; then exit; fi
...but this...
if [ -n $thing ] && [ $thing = "special_value" ]; then
do_the_special_thing
fi
comes out with
[: =: unexpected operator
I don't understad.
When $thing is null the first factor of the && evaluates to false and therefore the second should not be evaluated, but it appears that it is being evaluated?
I guess it's just a feature of sh that I have to write this as two ifs?
If you fix your quote problem (which #CharlesDuffy mentioned) - your bottom 3 lines work just fine:
if [ -n "$thing" ] && [ "$thing" = "special_value" ]; then
do_the_special_thing
fi
Also, you referred to variables being "null", but the relevant term is a variable that is "unset". In Bourne shell terminology, "null" refers to the empty string.

What's the meaning of if [ -z "$SPARK_HOME" ]; then [duplicate]

This question already has answers here:
Is there a list of 'if' switches anywhere?
(5 answers)
Closed 5 years ago.
I'm looking at the following code:
if [ -z $2 ]; then
echo "usage: ...
(The 3 dots are irrelevant usage details.)
Maybe I'm googling it wrong, but I couldn't find an explanation for the -z option.
-z string: True if the string is null (an empty string)
See https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions
-z
string is null, that is, has zero length
String='' # Zero-length ("null") string variable.
if [ -z "$String" ]
then
echo "\$String is null."
else
echo "\$String is NOT null."
fi # $String is null.
test -z returns true if the parameter is empty (see man sh or man test).
The expression -z string is true if the length of string is zero.

How can I check if a variable that need to contain a number has been set? [duplicate]

This question already has answers here:
How do I test if a variable is a number in Bash?
(40 answers)
How to check if a variable is set in Bash
(38 answers)
Closed 4 years ago.
Given the following code:
Max=
if [[ something exists.. ]]; then
Max=2
// .. more code that can changes the value of Max
fi
// HERE
How can I check at "HERE" if Max is equal to some number (setted)?
if [ -z "$Max" ]
then
echo "Max: not set"
else if [ $Max -eq some_number ]
then
echo "Max is equal to some number"
else
echo "Max is set but not equal to some number"
fi
or
if [ -n "$Max" -a "$Max" = "some_number" ]
...
Notice that the second example is doing a string comparison which can solve some headaches but may hurt the sensibilities of purists.
Cool !
Max is set by default to an empty value, that when used in an arithmetic context, returns zero. For example:
Try running echo $(( definitelyNotAGivenName)) it comes out as zero, cool!
You can use the round brackets for arithmetic comparing - see more here and here

Use string as bash variable name in alternative value expansion [duplicate]

This question already has answers here:
How can I look up a variable by name with #!/bin/sh (POSIX sh)?
(4 answers)
Closed 7 years ago.
How can I use the value of one variable as the name of another variable in an alternative value expansion (${var+alt}) in bash?
I would think that
#!/bin/bash
cat='dog'
varname='cat'
if [ -z ${`echo "${varname}"`+x} ]; then
echo 'is null'
fi
should be roughly equivalent to
#!/bin/bash
if [ -z ${dog+x} ]; then
echo 'is null'
fi
but when I try to do this, I get
${`echo "${cat}"`+x}: bad substitution
I guess part of the problem is that the subshell doing the command substitution doesn't know about $varname anymore? Do I need to export that variable?
My reason for doing this is that I learned from this answer how to check if a variable is null, and I'm trying to encapsulate that check in a function called is_null, like this:
function is_null {
if [ $# != 1 ]; then
echo "Error: is_null takes one argument"
exit
fi
# note: ${1+x} will be null if $1 is null, but "x" if $1 is not null
if [ -z ${`echo "${1}"`+x} ]; then
return 0
else
return 1
fi
}
if is_null 'some_flag'; then
echo 'Missing some_flag'
echo $usage
exit
fi
I'm not sure if I understand your problem.
If I got, what you need is eval command.
$ cat='dog'
$ varname='cat'
$ echo ${varname}
cat
$ eval echo \$${varname}
dog

Resources