Extracting value of a variable

Hi Experts,

I have one scenario here. I want to extract the value of a variable as mentioned below using a shell script (bash).

I am having a file say File A. Inside the file, I have a variable assigned a string value.
File A:
Var1="value of the variable"

In a particular script "variable_name.sh", I am getting the input as Var1. Inside the script I am sourcing the File as well.
Executed as ./variable_name.sh Var1

I want to store the Var1's value inside another variable in the script as mentioned below.
Var_val=$1
extracted_variable=`echo "$[$Var_val]"`

Output required:
value of the variable

But when I try this, it is returned as 0. If I assign any number to Var1 (say Var1=100) then it is returning the value as 100.

Could you please help me in extracting the string value that is stored in Var1?

Regards & Thanks,
Vivek

Not sure if this is what you are after:-
Longhand using OSX 10.7.5, default bash terminal...

Last login: Tue Sep 15 09:01:15 on ttys000
AMIGA:barrywalker~> echo "VAR1='This is a test.'" > /tmp/variable
AMIGA:barrywalker~> source /tmp/variable
AMIGA:barrywalker~> VAR_VAL=$VAR1
AMIGA:barrywalker~> echo "$VAR_VAL"
This is a test.
AMIGA:barrywalker~> _

man bash :

If you evaluate a text variable, it will evaluate to 0. Quote the variable when used as a parameter to a function as it contains spaces. You don't need the echo when assigning to a new variable.

Hi Wisecracker,

I am not assigning as such.
AMIGA:barrywalker~> VAR_VAL=$VAR1

I am assigning like VAR_key=$1 ($1 is the input to the script)
$1 will be VAR1

I want to retrieve the string that is present in VAR_key. That is what I am trying to do here,
extracted_variable=`echo "$[$Var_key]"`

<B>@RudiC </B> : Variable will not have a text with spaces. It will be a string like this.
"THIS_IS_A_TEXT".

Thanks.

:confused:

The way to "retrieve" a variable in shell is $.

# VAR="value"
# echo $var

value

#

My requirement in simple words,

Var1="Value"
Var2=Var1

I want to assign "Value" to Var2. As per my original post, If I have a number instead of "value" then I am able to retrieve it.

Thanks

Do you mean like this:-
OSX 10.7.5, default terminal using 'sh'...

#!/bin/sh
# value
$1
echo $VAR1

Manually generate another file, (your File A), for testing and run 'value' file...

Last login: Tue Sep 15 19:47:17 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 value
AMIGA:barrywalker~/Desktop/Code/Shell> echo "VAR1=125" > /tmp/variable
AMIGA:barrywalker~/Desktop/Code/Shell> ./value 'source /tmp/variable'
125
AMIGA:barrywalker~/Desktop/Code/Shell> _

If this is NOT what you want the I have no idea what you have in mind...

So you want to turn a variable name into its contents?

If you have a new enough shell, you can do this:

VAR="asdf"
VARNAME="VAR"

echo "${!VARNAME}"

You can do the opposite, setting any variable name you want, with this trick:

VARNAME="VAR"

read "$VARNAME" <<EOF
VALUE
EOF

echo $VAR
1 Like

DON'T use arithmetic expansion when assigning a variable's contents to another var. Use VAR2="$VAR1" .

If you have no idea what the variable name is from the sourced file nor its contents then you could diff the ENVIRONMENT before and after sourcing this would give the variables and their contents.
However here is an idea that finds the variable name, its contents and transfers the contents into another variable...

#!/bin/sh
# value
# Create a simple test file to open up...
echo 'VAR1=125' > /tmp/variable
# VAR1=125 sourced from $1...
$1
# Obtaining the filename from $1, firstly create a simple array.
VAR2=( $1 )
# The second part of the string is the /full/path/to/filename.
VAR2=${VAR2[1]}
# Save the contents of the file to variable VAR2, VAR1 belongs to the sourced file.
VAR2=$(cat $VAR2)
# Show the variable and its value.
echo $VAR2
# Create a new local variable.
MYVAR=""
# Find the sourced variable name.
for n in $( seq 0 1 ${#VAR2} )
do
	if [ "${VAR2:$n:1}" == "=" ]
	then
		break
	fi
	MYVAR=$MYVAR${VAR2:$n:1}
done
echo "The sourced variable name is $MYVAR..."
# Now dump the contents of $MYVAR, 'VAR1' into a reused variable VAR2.
eval VAR2='$'"$MYVAR"
echo "The sourced variable value is $VAR2..."

Results:-

Last login: Tue Sep 15 19:48:21 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./value 'source /tmp/variable'
VAR1=125
The sourced variable name is VAR1...
The sourced variable value is 125...
AMIGA:barrywalker~/Desktop/Code/Shell> _

If you want to get all the variables, their contents and transfer to your local variables from a script then this is much more difficult...

Hi,

I have again explained my requirement.

FileA has this content:

Variable1="Value";

In the script, which is invoked by a single parameter.
./value_extractor.sh Variable1

. FileA
Retrieved_value=`echo $[$1]`
echo $Retrieved_value
exit 0

Please read my previous post. I explain how to use a variable containing a variable name to get the contents of that other variable.

1 Like

Puzzled; is this what you are after?
OSX 10.7.5, default terminal running 'sh'...

#!/bin/sh
# Value_extractor.sh Variable1
echo 'Variable1="This is a string...";' > /tmp/FileA
. /tmp/FileA
eval echo '$'"$1" > /tmp/someval
Retrieved_value=$( cat /tmp/someval )
echo $Retrieved_value

Results:=

Last login: Thu Sep 17 20:15:40 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 Value_extractor.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./Value_extractor.sh Variable1
This is a string...
AMIGA:barrywalker~/Desktop/Code/Shell> _
1 Like

Thanks Corona688.. The '!' helped us to retrieve the value. Thanks again.

As an addendum an example of multiple variables...
OSX 10.7.5, default terminal using 'sh'...

#!/bin/sh
# Value_extractor.sh Variable[?]
echo 'echo "Sourcing..."' > /tmp/FileA
echo 'Variable1="This is a string...";' >> /tmp/FileA
echo 'Variable2="My_name_is_Bazza..."' >> /tmp/FileA
echo 'Variable3="1234567890"' >> /tmp/FileA
echo 'echo "Sourcing done!"' >> /tmp/FileA
. /tmp/FileA
# eval echo '$'"$1" > /tmp/someval
# Retrieved_value=$( cat /tmp/someval )
Retrieved_value=$( eval echo '$'"$1" )
echo $Retrieved_value

Results:-

Last login: Fri Sep 18 09:18:02 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./Value_extractor.sh Variable1
Sourcing...
Sourcing done!
This is a string...
AMIGA:barrywalker~/Desktop/Code/Shell> ./Value_extractor.sh Variable2
Sourcing...
Sourcing done!
My_name_is_Bazza...
AMIGA:barrywalker~/Desktop/Code/Shell> ./Value_extractor.sh Variable3
Sourcing...
Sourcing done!
1234567890
AMIGA:barrywalker~/Desktop/Code/Shell> _