0

In the bash shell,

$ date +%Y%m%d%H%M
202208161535

So I put it in a bash script

#!/bin/bash
dat=${date +%Y%m%d%H%M}
echo "dat=" ${dat}

But when I run it, I get

$ test.bash
./test.bash: line 2: ${date +%Y%m%d%H%M}: bad substitution
dat=

How should I do it?

ADD
I found

dat=`date +%Y%m%d%H%M`

works. But I'm curious how I can do it with dat = ${data +%Y%m%d%H%M}.

ADD2
This question arose because of the mistake, or rather from not knowing the difference of ( ) and { }. Those who cannot notice this difference cannot search with search pattern 'difference of ( ) and { } in bash'. So the referenced links 'supposed to have solution for this question' cannot be searchable by the people like me. So I think this question is worth being kept as is.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112

1 Answers1

-1
#!/bin/bash
dat=`date +%Y%m%d%H%M`
echo "dat="${dat}

The above code is working code

Raja M
  • 14
  • 1
  • 4
    Do not use backticks. Use `$(..)` instead. See https://wiki.bash-hackers.org/scripting/obsolete . Also this is not working code - the last line has unclosed backtick. Remember to check your scripts with shellcheck – KamilCuk Aug 16 '22 at 07:25
  • 1
    Hi Raja, thanks but I knew how to use back ticks when I asked it. I wanted to know $( ) method. – Chan Kim Aug 16 '22 at 08:43
  • You are needlessly quoting a static string which does not need quoting, and failing to quote a variable which might need quoting. See also [When to wrap quotes around a shell variable](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Aug 16 '22 at 10:21