how to combine multi bash parameter expansion [duplicate] - bash

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.

Related

What does the #*$ in a shell script's string interpolation do? [duplicate]

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.

Extract the pointer from a V_${i}_T variable name in a bash loop [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
Suppose I have several variable names like
V_1_T = a
V_2_T = b
V_3_T = c
...
and I want to extract the pointers a, b , c, ... in a bash loop in order to concatenate the values. My explicit wish is about reconstructing a message separated in several parts as explained in the gammu-smsd documentation.
I've tried the example in the doc but it doesn't work. The reason is that the code never points to the pointer of the variables but to the variables themselves, i.e. I get V_1_T at best and never a as I would.
I've also tried to put
${V_${i}_T} ; ""$"V_${i}_T"
with and without escape symbols for the commas, ..., but nothing worked ...
Any ideas ?
I'm working on the latest version of Raspbian + RaspberryPi.
Use indirect parameter expansion:
for i in 1 2 3; do
t="V_${i}_t"
echo "${!t}"
done
This avoids the use of eval shown in the docs you linked to.

Bash scripting help ENV_NAME=${1:-develop} [duplicate]

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".

Double expansion of a parameter in Bash script [duplicate]

This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
Closed 7 years ago.
I want to expand a parameter twice, and ${$PARAM} doesn't work.
For example, if I set two variables to file names and then want to loop through those variables and expand to their file names:
INPF_1=input1.inp
INPF_2=input2.inp
# copy the input files to the execution directory
for input_param in ${!INPF_*}; do
echo ${$input_param}
done
How can I to access the file names from those parameters in the for loop?
I.e., expand to input1.inp and input2.inp.
You had it almost: just use "${!input_param}" instead of ${$input_param}.
The quoting doesn't do anything in this case, but it's a good habit.
Be aware of the difference to ${!INPF_#}, which – when used between double quotes – expands to a separate word for each variable name, whereas "${!INPF_*}" doesn't. You almost always want "${!INPF_#}". See the difference:
$ for var in "${!INPF_*}"; do echo "$var"; done
INPF_1 INPF_2
$ for var in "${!INPF_#}"; do echo "$var"; done
INPF_1
INPF_2
See the manual for the various parameter expansions.

why we use ##*/ expression with bash variable [duplicate]

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.

Resources