This question already has answers here:
How to check if a variable is set in Bash
(38 answers)
Closed 5 years ago.
I've come across code that uses this syntax in an if condition:
if [ ! -z ${VARIABLE+x} ]; then
some commands here
fi
Does it test for an non-empty variable? If so, how is it different from ! -z "$VARIABLE"?
See PARAMETER EXPANSION in man bash:
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
And few paragraphs above in the same section:
Omitting the colon results in a test only
for a parameter that is unset.
Related
This question already has answers here:
What does "plus colon" ("+:") mean in shell script expressions?
(4 answers)
Closed 5 years ago.
I have a bash script that uses the following syntax:
if [ ! -z ${ARGUMENT+x} ]; then
What is the meaning of the "+x" syntax after the argument name?
It means that if $ARGUMENT is set, it will be replaced by the string x
Let's try in a shell :
$ echo ${ARGUMENT+x}
$ ARGUMENT=123
$ echo ${ARGUMENT+x}
x
You can write this with this form too :
${ARGUMENT:+x}
It have a special meaning with :, it test that variable is empty or unset
Check bash parameter expansion
Rather than discussing the syntax, I'll point out what it is attempting to do: it is trying to deterimine if a variable ARGUMENT is set to any value (empty or non-empty) or not. In bash 4.3 or later, one would use the -v operator instead:
if [[ -v ARGUMENT ]]; then
This question already has answers here:
Assigning default values to shell variables with a single command in bash
(11 answers)
Closed 2 years ago.
I understand the empty string in Bash is falsy, and we should be able to use something like
a=""
b=($a || 0)
which means if $a is falsy, then just make it 0. But it gave
bash: syntax error near unexpected token `||'
and I understand we can use [-z $a] and use the Bash ternary form:
a=""
[[ -z $a ]] && b=0 || b=$a
But is there a way to use something similar to the first form above that works in both Bash and Zsh?
You can use parameter expansion: ${PARAMETER:-WORD} will, if the variable PARAMETER is unset or an empty string, evaluate to WORD, otherwise the value of PARAMETER.
So:
b=${a:-0}
This question already has an answer here:
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Closed 4 years ago.
Consider the following Bash script:
function dosomething {
local fname="~/.bash_profile"
if [[ -f "$fname" ]]; then
echo "proceeding"
else
echo "skipping"
fi
}
dosomething
I always get "skipped" although I know that ~/.bash_profile exists. Why?
~ is only expanded by the shell if it's unquoted. When it's quoted it's a literal tilde symbol.
local fname=~/.bash_profile
This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 5 years ago.
I seem at example 9-14.
$ echo ${1:-0}
$ 0
$ echo ${2:-32767}
$ 32767
So I cant understand...
Per the Bash Reference Manual, §3.5.3 "Shell Parameter Expansion":
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
So, for example, this script:
echo "${foo:-1}"
foo=2
echo "${foo:-3}"
foo=
echo "${foo:-4}"
prints
1
2
4
The syntax is ${var:-$DEFAULT}. It means if the variable is not set or is null, use the default value.
This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 6 years ago.
I have a code block with below condition, not sure what exactly it does.
$var = "${args}_Some_Text"
if [ "${!var}" == '' ];then
echo "$var is not defined !!!"
fi
This is called variable indirect expansion.
$ hello="this is some text" # we set $hello
$ var="hello" # $var is "hello"
$ echo "${!var}" # we print the variable linked by $var's content
this is some text
As you see, it is a way to define "variable variables". That is, to use variables whose content is the name of another variable.
From Bash Reference Manual → 3.5.3 Shell Parameter Expansion:
If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. If parameter is a nameref, this expands to the name of the variable referenced by parameter instead of performing the complete indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[#]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.