0

When I try the following there is no problem:

$ date '+%y%m' | sed 's/.\(.*\)/\1/'
906

But When I try to store it in a variable it seems nothing is stored:

$ dd=`date '+%y%m' | sed 's/.\(.*\)/\1/'`
$ echo "$dd"

I know I can get the same result by not using sed:

$ dd=`date '+%y%m'`
$ echo ${dd: -3}
906

but I'm curious why it doesn't work with sed.

7
  • 2
    It seems to work fine. Which shell are you using
    – Inian
    Jun 4, 2019 at 19:33
  • I'm using git-bash for Windows.
    – LoMaPh
    Jun 4, 2019 at 19:34
  • It is working on the git-bash also! are you printing the right variable name?
    – Inian
    Jun 4, 2019 at 19:35
  • Yes. Pretty sure!
    – LoMaPh
    Jun 4, 2019 at 19:36
  • 3
    Prefer dd=$( ... ) syntax instead of dd=` ... `
    – FedKad
    Jun 4, 2019 at 19:37

1 Answer 1

-1

Don't use commands as a variable name. dd is your disc destroyer, look at man dd and try another variable name or something like this:

d=`date '+%y%m' | sed 's/.\(.*\)/\1/'`
echo $d

Maybe this part of the bash manual is also usefull:

man bash | sed -n '/^DEFINITIONS/,/^SHELL GRAMMAR/p' | less

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .