0

Is there any way I can print the variable itself not a result ?

x=`curl -s https://google.com`

Expected Result

curl -s https://google.com

Can any one suggest a way to get the above result?

Thanks

login
  • 1
  • 1
    use quotes instead of command substitution? x='curl -s https://google.com'. Seems like an XY problem. – jesse_b Feb 08 '20 at 14:23

1 Answers1

1

The variable is never set to:

`curl -s https://google.com`

The command substitution happens before the variable assignment, and then the variable is assigned it's output. I suppose you may be able to read from the file to get it:

#!/bin/bash

x=$(echo foobar)

awk -F\= '$1 == "x"{gsub(/\$\(|\)/, ""); print $2}' "$0"

Note you should use $(...) command substitution instead of backticks for many reasons, and you probably shouldn't do this at all. If you tell us what your actual intent is there is surely a better solution.


You could also store your command in an array and use that for reference:

#!/bin/bash

x=(echo foobar)

y=$("${x[@]}")

printf 'x is: %s\ny is: %s\n' "${x[*]}" "$y"

Which will output:

$ ./script.sh
x is: echo foobar
y is: foobar
jesse_b
  • 37,005