1
USER_UID=$1
echo 'generate_token("$USER_UID")'

I want output like

generate_token("1234567")

i tried multiple ways but didn't worked. it just print same line without value generate_jwt("$USER_UID")

bugCracker
  • 3,656
  • 9
  • 37
  • 58

1 Answers1

4

When you use single quotes, it causes the shell to preserve the literal value of each character within the quotes. This means the $ will be treated as a literal $ character.

You should use double quotes:

USER_UID="$1"
echo "generate_token(\"$USER_UID\")"

From the bash man page, under the Quoting section:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.

For POSIX details on quoting, see here.

Example in an interactive shell:

$ USER_UID='foo'
$ echo "generate_token(\"$USER_UID\")"
generate_token("foo")

This will also work if USER_UID contains spaces:

$ USER_UID='var with spaces'
$ echo "generate_token(\"$USER_UID\")"
generate_token("var with spaces")
Shane Bishop
  • 3,905
  • 4
  • 17
  • 47
  • @JohnBollinger Thanks for pointing that out, I updated my answer to correctly add the double quotes in the output. – Shane Bishop Jan 24 '21 at 16:22
  • Works for me, though as a matter of style and to avoid the possibility of `$USER_UID` being subject to word splitting, I would personally use `'generate_token("'"$USER_UID"'")'` or, better, `"generate_token(\"$USER_UID\")"`. – John Bollinger Jan 24 '21 at 16:24
  • echo "generate_token(\"$USER_UID\")" this worked fine. – bugCracker Jan 24 '21 at 16:25