I've been refactoring some bash code, and stumbled upon this bash notation:
"${string_a:-string_b}"
I've played a little with this on the command line:
$ echo "${string_a:-string_b}"
string_b
$ export string_a=string_a_value
$ echo "${string_a:-string_b}"
string_a_value
I seems that the {a:-b} notation returns the value of variable a if it is defined, or the string b otherwise.
Where can I find a more formal definition for this operator?
Peer pressure, I post my comment as an answer : )
I like this reference card: Advanced Bash-Scripting Guide , specifically in your case it will be useful "# Table B-4. Parameter Substitution and Expansion".
I do not copy any issue they indicate not to violate any copyright. Just find all information there.
Another useful link is the Shell Parameter Expansion section in the Bash Reference
Manual. The :- operator is defined as:
${parameter:-word} If parameter is unset or null, the expansion of
word is substituted. Otherwise, the value of parameter is substituted.
By the way, bash features three similar operators ${parameter:=word}, ${parameter:?word} and ${parameter:+word}, defined in that section.
You can access bash documentation using man bash. To search type /
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
Related
I stumbled across this post where user chepner proposed in his answer the usage of \j (as mentioned in the bash manual) to retrieve the current running count of background jobs.
Basically it boils down to
num_jobs="\j"
echo ${num_jobs#P}
Can anyone enlighten me on what is going on here exactly? E.g.
why ${\j#P} is not working and
what #P is doing exactly?
Like any parameter expansion, you have to supply the name of a parameter, not an arbitrary string. \j isn't the name of a parameter; it's the text you want to get from a parameter expansion.
After the parameter has been expanded, #P further subjects the result to prompt expansion, so that \j is replaced by the number of jobs.
$ num_jobs="\j"
$ echo "${num_jobs}"
\j
$ echo "${num_jobs#P}"
0
The part before the # is the name of the parameter you're trying to expand, it can't be a string you want to modify somehow. And #P is a parameter expansion introduced in Bash 4.4 (see manual):
${parameter#operator}
The expansion is either a transformation of the value of parameter
or information about parameter itself, depending on the value of
operator. Each operator is a single letter:
P
The expansion is a string that is the result of expanding the value of
parameter as if it were a prompt string (see Controlling the Prompt).
I came across this expression ${varname:-text},And 3 other similar forms:
${varname-text}
${varname:=text}
${varname:?text}
It seems like text substitution, how to use this expression?
The standards document for the POSIX shell explains these special parameter expansion formats thusly:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of
word shall be substituted; otherwise, the value of parameter shall be
substituted.
${parameter:=word}
Assign Default Values. If parameter is unset or null, the expansion of
word shall be assigned to parameter. In all cases, the final value of
parameter shall be substituted. Only variables, not positional
parameters or special parameters, can be assigned in this way.
${parameter:?[word]}
Indicate Error if Null or Unset. If parameter is unset or null, the
expansion of word (or a message indicating it is unset if word is
omitted) shall be written to standard error and the shell exits with a
non-zero exit status. Otherwise, the value of parameter shall be
substituted. An interactive shell need not exit.
There are examples given there and a more complete table. All POSIX-compliant shells must support them. Some, like bash, include additional formats.
My question is: What is v1,v2,v3,v4 and v5 in the below function?
They look like input parameters but I am not really sure what they are for. Can anyone explain it in a bit detail of what they do?
#!/bin/sh
compile()
{
v1="$1*z*"
v2=${2:-"$1*"}
v3=${3:-"$1*/"}
v4=${4:-"."}
v5=${5:-"."}
some other command and cd in here
}
compile libpng
compile icu "" "" source build
.
.
.
$1 etc. are the arguments to the function.
And, from the bash(1) man page, EXPANSION section, Parameter Expansion subsection:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expan‐
sion of word is substituted. Otherwise, the value of parameter
is substituted.
I'm trying to understand a bash script whose first four lines are:
#!/bin/sh
SCRIPT="`basename $0 | sed 's/\..*$//'`"
CONFIG=${1:-$HOME/.$SCRIPT}
DIR=${2:-$HOME/Documents}
I understand that the last two lines are doing parameter substitution on paths input as script arguments 1 and 2, but I've been unable to figure out how this works (e.g. here). What does the ":-" part mean? Sorry for the newbie question.
From man bash:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Other‐
wise, the value of parameter is substituted.
Very easy to find, with man bash, and then /:-. The slash introduces a search, and :- is just the content to search for. Else, searching in bash can get very boring, because it is huge, but here it is the first hit.
Does anyone know what is := for?
I tried googling but it seems google filters all symbol?
I know the below is something like checking if the variable HOME is a directory and then something is not equal to empty string.
if [ "${HOME:=}" != "" ] && [ -d ${HOME} ]
From Bash Reference Manual:
${parameter:=word}
If parameter is unset or null, the expansion of word is assigned to
parameter. The value of parameter is
then substituted. Positional
parameters and special parameters may
not be assigned to in this way.
Basically it will assign the value of word to parameter if and only if parameter is unset or null.
From the Bash man page:
Assign Default Values. If
parameter is unset or null, the
expansion of word is assigned to
parameter. The value of parameter
is then substituted. Positional
parameters and special parameters may
not be assigned to in this way.
Man pages are a wonderful thing. man bash will tell you almost everything you want to know about Bash.