Double expansion of a parameter in Bash script [duplicate] - bash

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.

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.

How to make a string, not a value of a variable but a new variable? [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed last year.
I have these variables:
var1=ab
var2=cd
result=${var1}-text-${var2}
ab-text-cd=bingo
I have:
$ echo $result
ab-text-cd
I would like to have:
$ echo $result
bingo
Is it possible and how?
More info:
Var1 and var2 are arguments given to script.
Thanks to #Léa Gris.
I didn't know about "indirect parameter expansion".
"If the first character of PARAMETER is an exclamation point, Bash uses the value of the variable formed from the rest of PARAMETER as the name of the variable."
Solution :
result2=$(echo ${!result1})
You can use eval to achieve this. Be warned though, that using eval is almost always a bad idea, as it has glaring security issues (rooted in its design -- it is meant to execute everything passed to it) and even apart from that, all kinds of things might go wrong when a variable has an unexpected value.
result=${var1}-text-${var2}
eval ${var1}'_text_'${var2}=bingo
echo $ab_text_cd
Also, environment variables cannot have dashes (-) as part of the variable name, so I replaced them by underscores (_) for the example.

bash variable eats multiple spaces, turning them to one [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
# export var="many spaces"; echo =${var}=
=many spaces=
What is going on here?
Why multiply spaces are turned to one? How to keep all?
You’re simply missing quotations around your variable. Changing your code to this:
$ export var="many spaces"; echo ="${var}"=
=many spaces=
should give the result you’re looking for. One “feature” of bash that you need to watch out for is word splitting, which is based on the value of your IFS (internal field separator) variable. Typically IFS defaults to
IFS=$' \t\n'
so you need to take care in quoting variables that contain spaces, tabs, and newlines.

bash script whole file reading into a variable with newline [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 7 years ago.
i want to read whole file in a variable.
for example my file is.
file name : Q.txt
My name is Naeem Rehmat.
I am a student of FAST university.
Now i am in 4th semester.
I am learning bash script.
I have this problem.
code:
text=$(cat Q.txt)
echo $text
out put should be like this:
My name is Naeem Rehmat.
I am a student of FAST university.
Now i am in 4th semester.
I am learning bash script.
I have this problem.
Presumably the problem is that your whitespace is incorrect. Use double quotes:
echo "$text"
When you write echo $text without quotes, bash evaluates the string and performs what is known as "field splitting" or "word splitting" before generating the command. To simplify the case, suppose text is the string foo bar. Bash splits that into two "words" and passes "foo" as the first argument to echo, and "bar" as the second. If you use quotes, bash passes only one argument to echo, and that argument contains multiple spaces which echo will print.
Note that it is probably good style to also use quotes in the assignment of text (ie, text="$(cat Q.txt)"), although it is not necessary since field splitting does not occur in a variable assignment.

Appending text to the end of a variable [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 8 years ago.
The following works, but I don't want the space that it returns:
read input
file= "$input"
file= "$file ins.b" # how to get rid of the space here?
echo "$file"
This outputs 'file ins.b'
I don't want the space between file and ins.b
If I don't leave that space in the code it returns only '.b'. What can I do to resolve this problem?
Append like:
file="${file}ins.b"
If you don't use braces then it treats fileins as a variable and expands it. Since, it's probably not set it just prints .b.
Related: When do we need curly braces in variables using Bash?
In bash you can also reference variables like ${file}. So this should work for you:
file="${file}ins.b"
You don't need to expand the old value at all; bash has a += operator:
file+="ins.b"
file="${file}ins.b"
or
file=$file"ins.b"

Resources