4

I have been trying to replace '{{date}}' with a current date stamp in OSX command line. I have been using the following:

sed -i -e 's/{{date}}/`date`/g' mhp.xml

Does anyone know why it ends up putting

`date`

instead of the actual date?

When I try

date=`date`
echo $date

it works... and shows the current date. Any ideas?

1
  • 1
    The reason WHY your version didn't work is because you used single-quotes around the sed script rather than double-quotes. text inside single-quotes is treated as a fixed string literal. Text inside double-quotes is interpolated by the shell, with variable expansions, command substitution (like $(date)), etc applied. Both forms are useful, sometimes you want string literals, sometimes you want interpolation, sometimes you want both (which requires careful use of quoting and escaping). – cas Mar 25 '16 at 3:29
9

With GNU sed:

sed -i "s/{{date}}/$(date)/g" mhp.xml

With BSD sed:

sed -i '' "s/{{date}}/$(date)/g" mhp.xml
3
  • 1
    Ok so the BSD one worked on mac osx but do those single quotes signify? – petrosmm Mar 25 '16 at 3:27
  • 1
    the -i option on BSD sed requires an argument. '' gives it an empty string as its argument. GNU sed supports an optional argument for -i so doesn't require the '' empty string. – cas Mar 25 '16 at 3:31
  • @MaximusPeters: BSD sed requires those to empty suffix edit in-place work, see unix.stackexchange.com/q/92895/38906 – cuonglm Mar 25 '16 at 3:32

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