0

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.

5
  • Edit: Please one question per question. Please do not create follow up questions. Commented Oct 29, 2020 at 12:03
  • @jam : Are you sure that test.sh and the calling process access the same variable RES? Perhaps you forgot to export it? Commented Oct 29, 2020 at 12:06
  • As i posted and edited the question, the code block might have ignored the important part of it. I edited above. RES="secret" added line. Commented Oct 29, 2020 at 12:18
  • Or better RES=$(cat .secret) i have no read access. Commented Oct 29, 2020 at 12:27
  • Is there any way to get out of this cage to bypass this task ? If not, then i am sure it is secured correctly. Thanks for struggling on this issue. Commented Oct 29, 2020 at 12:49

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, i edited the post at the beginning... It was an example what i meant with the bypass. Exactly what i wanted to know is how to pass a variable as parameter and bypass the equality. ./test.sh $RES; then $1 should be $RES. But it still does not work.
"indirect variable reference" was helpful.
The main thing is how to make $1 to $RES, so "$1"="$RES" equality can be bypassed without knowing the inside of RES variable.
And by the way, i am not privileged enough to change the code, because of the level for this homework, all i can speculate is about dealing with the $x value.
@jam : There is no indirect variable reference going on in this example.
|
1

To copy a value from one variable to another one, use normal assignment:

x=$RES
test "$x" = "$RES" && echo Same

Double quotes expand variables, so "$RES" corresponds to the content of the variable $RES. If it doesn't contain the string $RES, the values are not equal.

Single quotes don't expand variables:

test "$x" = '$RES'

Or, backslash the dollar sign:

test "$x" = \$RES
test "$x" = "\$RES"

9 Comments

Hi choroba, i mean how can i make $x to $RES, so that i am able to pass the task.. thx
.. and in this case i am able to change only the x variable not RES...
@jam: I don't understand. What exactly do you want to achieve?
To assign the value from $RES to $x, just x=$RES.
i want to bypass $RES without having knowledge of what is inside.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.