How can a bash variable be written into a quoted variable ? For example;
x = '$RES'
when i echo this one, it returns $RES, but the code below ends with 1 (error)
test "$x" = "$RES" ; echo $?;
the command above should return 0 when it succeeds. What is wrong with that ?
thanks for a rapid reply,
Edit:
# this script is running with the rights of other user.(sudo)
# Usage: ./test.sh [password]
RES=$(cat .secret) # i have no read access right.
if [ ! -v "$1" ]; then
echo "Bad Luck! You are evil.."
exit 1
fi
if test "$1" = "$RES" ; then
echo "OK : $RES"
else
echo "Repeat it.."
fi
export x=RES
export RES=RES # i tried it in anyway like RES='$RES' and so on.
./test.sh $x
when i call a bash script with a parameter for example x and declare it by x=$RES it still does not bypass the equality.
There is no such thing as a quoted variable.
In your command
test "$x" = "$RES"
x
in your example has the value $RES
(i.e. consists of 4 characters).
On the right-hand side, you are using Double quotes, which interpolate the value of the variable (in this case, the variable RES
). You did not say what value RES
contains, but unless you explicitly have set
RES='$RES'
they will compare to not-equal. To compare to equality, you have to compare x
to the string $RES and not to the content of the variable RES
. You can do this either with
test $x = '$RES' # single quotes prevent interpolation
or
test $x = \$RES # backslash escapes interpolation