This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 11 months ago.
I am still snooping around bash as a newbie and I wanted to ask some question
Is it possible to append additional text to value in bash?
Example below:
#!/bin/bash
value1="qwerty"
value2="asdfgh"
echo $value1 >> $value3
echo $value2 >> $value3
echo $value3
So my question is - can I create value that contain other values?
Why am I trying to do this? Because I want to do some logical functions for each value and if it meets the criteria it gets appended to my final value.
By the end I would echo value3 and it would contain all results that met criteria.
>> is for writing to files, not appending to variables. You just want string interpolation.
value3="$value1$value2"
If you want an embedded newline, you can do that:
value3="$value1
$value2"
If you want to append a value to an existing variable,
value3=$value1
value3="$value3
$value2"
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:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 years ago.
I'm trying to assign a variable with a value I get from grep, here's my code
a="i
am
a
string
"
b="$a"|grep am
echo "$b"
I expect the result is am, but the result b is empty. But when I code echo "$a"|grep am directly, I get the right result. why and how can I assign the result to b?
Do it like this:-
**b=$(echo "$a"|grep am)
**
This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 5 years ago.
For string matching purposes I need to define a bash variable with leading spaces.
I need to define this starting from an integer, like:
jj=5
printf seems to me a good idea, so if I want to fill spaces up to 6 character:
jpat=`printf " %6i" $jj`
but unluckly when I am trying to recall the variable:
echo $jpat
the leading whitespaces are removed and I only get the $jj integer as it was.
Any solution to keep such spaces?
(This is equivalent to this: v=' val'; echo $v$v. Why aren't there leading and multiple spaces in output?)
Use More Quotes! echo "$jpat" will do what you want.
There is another issue with what you're doing: Command substitutions will remove trailing newlines. It's not an issue in the printf command you're using, but for example assigning jpat=$(printf " %6i\n" "$jj") would give you exactly the same result as your command.
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"
This question already has answers here:
Lookup variable value from string in shell script
(2 answers)
Closed 8 years ago.
I'm trying to find all variables which match a particular pattern and print their values.
test_a="apple"
test_b="banana"
test_c="carrot"
test_d="doughnut"
test_show_all () {
local i
for i in ${!test_*}; do
printf "..$i\n"
# printf "..$i-->${$i}\n"
done
}
The loop is finding the correct variables.
But if I uncomment the second line of the for loop, bash is unhappy with the syntax ${$i}. I thought this should work since $i holds the name of a variable, so I thought ${$i} should expand to value of that stored name.
The indirect variable reference is ${!var}. Change your code to
printf "..$i-->${!i}\n"
and it should work.
Not useful now, but in bash 4.3, you will also be able to use namerefs.
for name in ${!test_*}; do
declare -n value=$name
printf "..$name->$value\n"
done
As a short cut, if you only want to iterate over the values (without regard to the actual name of the variable), you can use
declare -n value
for value in ${!test_*}; do
printf "..$value\n"
done