5

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?

cuonglm
  • 153,898
petrosmm
  • 153
  • 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 03:29

1 Answers1

13

With GNU sed:

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

With BSD sed:

sed -i '' "s/{{date}}/$(date)/g" mhp.xml
cuonglm
  • 153,898
  • 1
    Ok so the BSD one worked on mac osx but do those single quotes signify? – petrosmm Mar 25 '16 at 03: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 03:31
  • @MaximusPeters: BSD sed requires those to empty suffix edit in-place work, see http://unix.stackexchange.com/q/92895/38906 – cuonglm Mar 25 '16 at 03:32