Bash script what is := for? - bash

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.

Related

what is the meaning of : ${CONTAINER_CLI:="docker"} in shell scripting

I am learning shell scripting and came across this line
: ${CONTAINER_CLI:="docker"}
can someone please explain me what this line do? what is the meaning of : here?
man bash command said:
${parameter:=word}
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.
So, the variable CONTAINER_CLI is assigned with the value docker if CONTAINER_CLI does not exists or empty.
But... if you simply write :
${CONTAINER_CLI:="docker"}
You obtain an error if the result is not a command.
You just want make a assignation.
Put simply a : before.
It's like:
CONTAINER_CLI=${CONTAINER_CLI:="docker"}

Is this if condition equivalent to testing the existence of a variable? [duplicate]

This question already has answers here:
What does the '-' (dash) after variable names do here?
(3 answers)
Closed 2 years ago.
I'm digging around in some the system files on my Mac. In /etc/profile I've found the following excerpt:
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
I've tried echo "${x-no}" for various choices of x and it seems like it's printing the value of x whenever x exists (i.e. has been set), and no otherwise.
Which leads me to wonder: Is this condition simply testing whether the variable x has been set?
Further questions: What exactly does - do? Is there a better way to test whether a variable has been set?
The meaning of ${BASH-no} is documented in §2.6.2, Parameter Expansion, of the Single Unix Specification:
${parameter:-[word]}
Use Default Values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be substituted; otherwise, the value of parameter shall be substituted.
The meaning when the colon is omitted (as in your example) is described slightly later:
In the parameter expansions shown previously, use of the in the format shall result in a test for a parameter that is unset or null; omission of the shall result in a test for a parameter that is only unset.
Then there is a table, which may be easier to understand. Here are the relevant rows:
parameterSet and Not Null
parameterSet but Null
parameterUnset
${parameter:-word}
substitute parameter
substitute word
substitute word
${parameter-word}
substitute parameter
substitute null
substitute word
Here is a reliable, portable way to check whether a variable is not set at all. Note that I am using a + modifier instead of a - modifier in the parameter expansion:
if [ "${BASH+set}" = "" ]; then
echo 'BASH not set at all'
else
echo 'BASH is set, perhaps to the empty string'
fi
The expansion of "${BASH+set}" can only be "" if BASH is entirely unset. If BASH is set, even to the empty string, then "${BASH+set}" expands to "set" instead.
Is this condition simply testing whether the variable x has been set?
Yes, though it gets confused in the unlikely event that BASH=no.
What exactly does - do?
Here's man bash:
[...] Omitting the colon results in a test only for a parameter that is un‐set.
${parameter:-word}
Use Default Values. If parameter is unset or null (see above),
the expansion of word is substituted. Otherwise, the
value of parameter is substituted.
Is there a better way to test whether a variable has been set?
Yes: [ -v BASH ]. However, this is bash specific, so it defeats the purpose of checking if the current shell is bash before doing bash specific operations.

bash: "${A:-B}" operator

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.

What does the special form ${varname:-text} do in shell script?

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.

Shell script variables I saw but not sure what they mean

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.

Resources