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.

dude
  • 117
  • 6
  • `Edit:` Please one question per question. Please do not create follow up questions. – KamilCuk Oct 29 '20 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? – user1934428 Oct 29 '20 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. – dude Oct 29 '20 at 12:18
  • Or better RES=$(cat .secret) i have no read access. – dude Oct 29 '20 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. – dude Oct 29 '20 at 12:49

2 Answers2

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"
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Hi choroba, i mean how can i make $x to $RES, so that i am able to pass the task.. thx – dude Oct 28 '20 at 23:31
  • .. and in this case i am able to change only the x variable not RES... – dude Oct 28 '20 at 23:35
  • @jam: I don't understand. What exactly do you want to achieve? – choroba Oct 28 '20 at 23:35
  • To assign the value from $RES to $x, just `x=$RES`. – choroba Oct 28 '20 at 23:36
  • i want to bypass $RES without having knowledge of what is inside. – dude Oct 28 '20 at 23:37
  • What do you mean by "bypass"? – choroba Oct 28 '20 at 23:39
  • i mean with bypass that it is a challenge homework, i developed. Thanks for your reply. I will try it harder. For now it is okay. – dude Oct 28 '20 at 23:42
  • @jam Are you trying to do an [indirect variable reference](https://stackoverflow.com/questions/1694196/lookup-shell-variables-by-name-indirectly)? – Gordon Davisson Oct 29 '20 at 00:29
  • I am not interested in the value of RES. I just want to bypass the equality. I want to pass the RES variable to x as parameter and make the code to see that there is already the variable RES and not to claim that there is something wrong with that. – dude Oct 29 '20 at 11:50
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
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • 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. – dude Oct 29 '20 at 11:31
  • "indirect variable reference" was helpful. – dude Oct 29 '20 at 11:35
  • The main thing is how to make $1 to $RES, so "$1"="$RES" equality can be bypassed without knowing the inside of RES variable. – dude Oct 29 '20 at 11:44
  • 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. – dude Oct 29 '20 at 11:52
  • @jam : There is no indirect variable reference going on in this example. – user1934428 Oct 29 '20 at 11:58
  • @jam : If you want the test `"$1" = "$RES"` to succeed, the parameter must contain the exact content of the variable `RES`. Assuming that RES is an environment variable in the calling process, you would simply pass `"$RES"` as parameter. In this case, to save yourself headache, I suggest to do a `set -u`, to catch cases where `RES` is not set. – user1934428 Oct 29 '20 at 12:00