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.
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:
Bash expand variable in a variable
(5 answers)
Closed 1 year ago.
I have several variables that have the name format of:
var0_name
var1_name
var2_name
And I want to be able to loop thru them in a manner like this:
for i in {0..2}
do
echo "My variable name = " ${var[i]_name}
done
I have tried several different ways to get this expansion to work but have had no luck so far.
Using the ${!prefix*} expansion in bash:
#!/bin/bash
var0_name=xyz
var1_name=asdf
var2_name=zx
for v in ${!var*}; do
echo "My variable name = $v"
# echo "Variable '$v' has the value of '${!v}'"
done
or equivalently, by replacing the for loop with:
printf 'My variable name = %s\n' ${!var*}
You may also consider reading the Shell Parameter Expansion for detailed information on all forms of parameter expansions in bash, including those that are used in the answer as well.
This question already has answers here:
Why can't I set IFS for one call of printf
(2 answers)
Closed 3 years ago.
Consider this bash session:
set -- 1 2 3
echo "$*" # 1 2 3 (as expected)
IFS=a echo "$*" # 1 2 3 (why not "1a2a3"?)
IFS=a; echo "$*" # 1a2a3a (as expected)
Try it online!
Why does the "assignment before command" syntax not work to set IFS to a different value for the duration of the single command? It appears you must change the value of IFS first with a separate command.
Because IFS affects the parsing (tokenization, word-splitting) of a command, so it must be set before that command is parsed. This is why IFS=a echo "$*" can only use the original IFS, not a.
It's a somewhat similar case to FOO=bar echo $FOO not echoing bar. $FOO is substituted (empty) and then the command, with it's variable assignment is executed.
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.
This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Closed 2 years ago.
a=2
a=3 echo $a #prints 2
can someone explain why would anyone use the above code in line-2.
a=3 will be ignored as there is no "enter" after it.
But I saw it in script like above and not sure about the purpose.
$a is expanded by the shell (Bash) before a=3 is evaluated. So echo sees its argument as 2, which is what it prints. (If you set -x you can see that what gets executed is a=3 echo 2.)
var=val command is used to set an environment variable to be seen by command during its execution, but nowhere else. So when command reads environment variables (e.g. using getenv()), to it $var is val.
If echo were to look up $a while running, it would have the value 3.
The parent process expands a before the environment is setup in which it sets a different value (3) for a. Despite the fact that variable a set to 3 by the echo executes, the value was expanded already. So it's too late.
You can instead do:
a=3 bash -c 'echo $a'