Bash - Assigning existing Variable values to a New Variable. New Variable values does not return the assigned value - bash

I want to assign a value of the existing Variable to a new Variable in my Bash script.
The issues is that once the New variable gets assigned to a value of the existing Variable it returns none instead of returning the existing variable value. (see code below):
VAR1="Hello World"
VAR2="Let's concatenate"
VAR1+="$MyVar" # assigning to a new variable
echo "$VAR1"
echo "$MyVar" # This is the issue --> no value returned (intention is to return "Hello World")
The output is for this command (echo "$MyVar") is:
VAR1=
echo ''

You aren't defining a new variable named MyVar. You are appending the empty string resulting from the expansion of the non-existent variable to the value of VAR1.
You want
MyVar=$VAR1
to get the desired result.
An example of what += does do:
$ x=foo
$ echo "$x"
foo
$ x+=bar
$ echo "$x"
foobar

Related

Not able to assign var to $2 var from command line bash

I have the bash script. I have to assign to second passed var other var, or upper-cased content of the same var. When I try to do sth like this:
x=${2^^}
$2=$x)
I got: line 173: xxx=XXX: command not found
When I try this command : set -- ${2^^}
$2 seems to be..empty. When I echo it, terminal shows empty line.
How to fix it?
You can't assign to $2 directly, yo need to use set. Copy the $# array to a named array, change its second element, and use set to assign the values back:
arr=("$#")
arr[1]=${arr[1]^^}
set -- "${arr[#]}"
printf '%s\n' "$2"
set -- ${2^^} sets the value of upper-cased $2 to $1 and clears the remaining positional arguments.

assigning name to text file in shell script [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

What means PREFIX=${1:-daily} in shell script [duplicate]

This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 5 years ago.
What means these declarations in bash script:
PREFIX=${1:-daily}
PROFILE=${2:-backup}
The key here is to understand what Parameter-Expansion ${PARAMETER:-WORD} syntax means here,
${PARAMETER:-WORD}
If the parameter PARAMETER is unset (never was defined) or null (empty), this one
expands to WORD, otherwise it expands to the value of PARAMETER, as if it
just was ${PARAMETER}
In your case the ${PARAMETER} being the positional arguments passed to a function or script,
Use the below script for a better understanding,
myTestFunction() {
PREFIX=${1:-daily}
PROFILE=${2:-backup}
printf "PREFIX value %s PROFILE value %s\n" "$PREFIX" "$PROFILE"
}
myTestFunction "some" "junk"
myTestFunction
which produces a result as
$ bash script.sh
PREFIX value some PROFILE value junk
PREFIX value daily PROFILE value backup
Also see the expanded debugger version of the script as
$ bash -x script.sh
+ myTestFunction some junk
+ PREFIX=some
+ PROFILE=junk
+ printf 'PREFIX value %s PROFILE value %s\n' some junk
PREFIX value some PROFILE value junk
+ myTestFunction
+ PREFIX=daily
+ PROFILE=backup
+ printf 'PREFIX value %s PROFILE value %s\n' daily backup
PREFIX value daily PROFILE value backup
how shell substitutes the values to the variables when $1 or $2 is not passed.
The syntax is generally used when by default you want to configure a variable with a certain value at the same time make it dynamically configurable also.
Assigns value of first argument to PREFIX variable if this first argument exist, "daily" if it does not.
It means, assign the first argument (if present), else daily to variable PREFIX and assign the second argument (if present), else backup to variable PROFILE
eg:
$ cat file.sh
#!/bin/bash
PREFIX=${1:-daily}
PROFILE=${2:-backup}
echo $PREFIX
echo $PROFILE
For the following command line args given, output would be like:
$ ./file.sh
daily
backup
$ ./file.sh abc
abc
backup
$ ./file.sh abc xyz
abc
xyz

making variable with the values of another variable in unix shell scripting

I need to make variable using two other variable values.
#i have two variable in unix i.e.#
v_a=a
v_b=b
##now i want to combine the two values of the two variable and the result of that i want that as a variable means v_a_b and use it.##
echo 'v_'$v_a'_'$v_b #the output will be v_a_b and i want to use it like $v_a_b
Suppose you have the following vars:
v_a=a
v_b=b
v_a_b="I want this one"
v_a_c="Wrong one!"
How do you get I want this one using the values of v_a and v_b ?
You can use eval, but try to avoid that:
# don't do this
eval echo "\$v_${v_a}_${v_b}"
You need a new variable , lets call it c and get the value of the constructed var:
c="v_${v_a}_${v_b}"
echo "Using the exclamation mark for retrieving the valueCombined: ${!c}"
Another way is using printf:
printf -v c "v_%s_%s" "${v_a}" "${v_b}"
echo "Combined: ${!c}"
Use eval to explicitly pre-evaluate an expression.
for your question,
eval 'echo \$v_"$v_a"_"$v_b"'
should work fine,
echo \$v_"$v_a"_"$v_b" evaluates to echo $v_a_b, then the intermediate result is evaluated again, so in effect, we get the result of $(echo $v_a_b)
note: you might want to quote the variable $v_a_b:
eval 'echo \"\$v_"$v_a"_"$v_b"\"'
If I understand correctly, you want to concatenate the contents of variables v_a and v_b (along with some fixed strings 'v_' and '_') then store it in a variable named v_a_b.
v_a=a
v_b=b
v_a_b='v_'$a'_'$b
echo $v_a_b
The output is v_a_b.
v_a=a
v_b=b
v_a_b=c
eval V_XYZ='$'"v_"$v_a"_"$v_b
echo 'Value= '$V_XYZ
output will be c

Getting variable values from variable names listed in array in Bash

I'm trying to print values of multiple variables that are listed in a Bash array as evident in the minimal code example below.
#!/bin/bash
VAR1="/path/to/source/root"
VAR2="/path/to/target/root"
VAR3="50"
VARS=("VAR1" "VAR2" "VAR3")
for var in ${VARS[*]}; do
echo "value of $var is ${$var}"
done
This gives me an error
line 8: value of $var is ${$var}: bad substitution
I want the following output:
value of VAR1 is /path/to/source/root
value of VAR2 is /path/to/target/root
value of VAR3 is 50
My search on Google and SO was not very fruitful. Because of the indirection (i.e., var iterates over an array containing names of variables for which I want the values), I'm not able to precisely word my search. But any help is appreciated.
Use indirect reference as:
#!/bin/bash
VAR1="/path/to/source/root"
VAR2="/path/to/target/root"
VAR3="50"
VARS=("VAR1" "VAR2" "VAR3")
for var in ${VARS[*]}; do
echo "value of $var is ${!var}"
done
Output:
value of VAR1 is /path/to/source/root
value of VAR2 is /path/to/target/root
value of VAR3 is 50

Resources