Reputation: 110
I have a file that I would like to rename using sed. For simplicity purposes, this is what I am trying to do:
sh-4.3$ echo `date`
Thu 17 Sep 08:29:50 EAT 2015
sh-4.3$ echo `date` | sed 's/`date`/Today/'
Thu 17 Sep 08:29:58 EAT 2015
sh-4.3$
I expect it to echo "Today". What's the catch?
Upvotes: 2
Views: 5492
Reputation: 10039
I preconise to also change the separator, date info could have some meta character depending settings
date | sed "s~$( date )~Today~"
Upvotes: 0
Reputation: 113834
Inside single-quotes, backticks are not evaluated. Use double-quotes instead:
$ echo `date` | sed "s/`date`/Today/"
Today
For a variety of reasons, backticks are considered archaic. For all POSIX shells, use $(...)
:
$ echo $(date) | sed "s/$(date)/Today/"
Today
Although it may not be relevant to your larger script, for this simple command, echo
is not needed:
$ date | sed "s/$(date)/Today/"
Today
Note that this is fragile. If the seconds happen to tick over between the execution of one and the other date
commands, then this substitution will fail.
Upvotes: 3
Reputation: 3529
echo `date`
is not the same as
echo 'date'
backticks cause execution of the expression encased within them, single quotes are a plain string.
Since your echo is so simple, echo date
would work just as well.
Upvotes: 0