This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 9 years ago.
I can see that the following code is initializing FOO to localhost if BAR is empty, but what's exactly happening with the :-? What is it?
If BAR had a non-empty value, FOO would be assigned that value. If not, FOO would be assigned localhost. You can find more ways in Shell Parameter Expansion.
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted.
Otherwise, the value of parameter is substituted.
Related
This question already has answers here:
What is the meaning of the ${0##...} syntax with variable, braces and hash character in bash?
(4 answers)
Closed 11 months ago.
I found the following code in a shell script, but I am unsure of what the test condition is evaluating for
if test "${SOME_VAR#*$from asdf/qwer}" != "$SOME_VAR"; then
echo "##zxcv[message text='some text.' status='NORMAL']";
fi
The combination #*$ does not mean anything special. There are three special symbols and each of them has its own meaning in this context.
${SOME_VAR#abc} is a parameter expansion. Its value is the value of $SOME_VAR with the shortest prefix that matches abc removed.
In your example, abc is *${from} asdf/qwer. That means anything * followed by the value of variable $from (which is dynamic and replaced when the expression is evaluated), followed by a space and followed by asdf/qwer.
All in all, if the value of $SOME_VAR starts with a string that ends in ${from} asdf/qwer then everything before and including asdf/qwer is removed and the resulting value is passed as the first argument to test.
Type man bash in your terminal to read the documentation of bash or read it online.
This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 5 years ago.
Let var be a variable and it assigned a value /home/user as below
var=/home/user
when using this variable, i have seen it using both of the below format,
1) cd ${var}
2) cd ${!var}
what is the difference? for me second option is not working , if i echo second option returns empty.
In this case, it's indirect expansion(a), the var variable is expanded to create another variable name and then that is expanded again to get your eventual result:
pax$ abc=def
pax$ def=ghi
pax$ echo ${abc} # abc -> def, one level.
def
pax$ echo ${!abc} # abc -> def -> ghi, two levels.
ghi
From the bash man page:
If the first character of parameter is an exclamation point (!), 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.
(a) It can have other more complex effects in other situations, such as when you use ${!prefix*} or ${!name[#]} but your case is the simpler one.
This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 6 years ago.
I've inherited some bash scripts and I see this one liner
ENV_NAME=${1:-develop}
Can someone tell me what it's doing? I don't even know how to google this.
Thanks!
The construct is a so called parameter expansion. It expands to a default value if the variable itself is not set or null. The semantic is
${variable:-default value}
$1 is the first parameter passed to the script. If the parameter will be omitted ENV_NAME defaults to "develop".
This question already has answers here:
Can ${var} parameter expansion expressions be nested in bash?
(15 answers)
Closed 5 years ago.
bash parameter expansion has a few useful function, e.g. substring ,replace,upper,lower. how to combine these function without define temporary parameter?
f="abc.txt"
e=${f:4:3} #txt
echo ${e^^} #TXT
I define e to upper the txt. echo ${${f:4:3}^^} can not work. Is it possible omit e. if in java i can write
String f="abc.txt";
System.out.print(f.substring(4,7).toUpperCase());
even, i can
System.out.print("abc.txt".substring(4,7).toUpperCase());
No, this is not possible in bash AFAIK.
To make it possible we would need some sort of prioritization (along-with parsing logic changes) when more than 1 parameter expansion is specified, for which there is no code as of now.
This question already has answers here:
explain the linux regex for getting filename
(2 answers)
Closed 8 years ago.
I am tring to understand the bash script.
I am seeing ##* / expression with bash variable.
i.e ${foo##*/}
Can someone please tell me why we use that expression?
It's called "Parameter expansion". The variable $foo is searched for a substring matching the pattern */ (i.e. anything up to a slash) from the beginning (#), and what remains in the variable is returned. Doubling the #-sign makes the matching greedy, i.e. it tries to find the longest possible match.