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".
Related
This question already has answers here:
How to get a variable value if variable name is stored as string?
(10 answers)
Closed 1 year ago.
I want to be able to take the values of two variables and concatenate them together to form the identifier for another variable in a bash script.
final_answer="we did it"
one="final"
two="answer"
t="${one}_${two}"
echo ${$t} # would like this to echo we did it; currently give "${$t}: bad substitution"
Not sure this is possible but it seems like bash would have this capacity somehow.
Thank you!
$ echo "${!t}"
we did it
See http://mywiki.wooledge.org/BashFAQ/006#Indirection for details.
This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 6 years ago.
In Pycharm when we use variable e.g. $privateKey, we get the warning Simple variable usage as below snapshot and recommend us to turn to the syntax ${privateKey}
My question is why we get such warning? What is the risk to use simple variable like that?
When clicking more
Thanks to #Whymarrh. One answer is as below.
since "$foobar" would instead expand foobar
My answer is to separate/distinguish $myVar and notInVar in string "$myVarnotInVar"
In other words
myVar=122
echo "$myVarnotInVar" # will print empty string "" since undefined variable $myVarnotInVar
echo "${myVar}notInVar" # will print 122notInVar
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.
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.