I have the following code:

echo $a

I want the result of that echo be $(date), not the actual date. How to achive that?

  • Dare I ask why you would want to store a shell command substitution, unexpanded, in a variable? Storing code in variables is sometimes, but very seldom, the right thing to do.
    – Kusalananda
    Oct 5 '18 at 16:02
  • The real case was with the $PS1 variable. The thing is i wanted to know where was defined, so i needed the code to do a grep in files, but when i did echo "$PS1" it was interpreted. Anyway, i could find the location without doing this, but the question remained, if it was possible.
    – Nestoter
    Oct 5 '18 at 17:02

You can't with double quotes, as the assignment had taken place in the first line. Unless you escape the dollar sign:

$ a="\$(date)"
$ echo "$a"
$ eval echo "$a"
Fri 5 Oct 18:53:45 CEST 2018

And with single quotes, you might do these:

$ a='$(date)'
$ echo "$a"
$ eval echo "$a"
Fri 5 Oct 16:45:45 CEST 2018
  • 1
    of course you can, you just have to escape the $ Oct 5 '18 at 15:01
  • Hmm, so there is no way to tell a="$(date)" to print just the value, without beign interpreted? The question came because i'm echoing a variable and i'm getting the result, but i would like to know the code behind.
    – Nestoter
    Oct 5 '18 at 16:38
  • @Nestoter See my updated answer.
    – user147505
    Oct 5 '18 at 16:55

Use the '' insted of ""


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.