I've got a LaTeX file containing


And during CI I want to do the following:

sed -i 's/{\\revision}{\(\w*\)}/\1'"$REV"'/' variables.tex

expecting something along the lines of \newcommand{\revision}{577f813d}

Unfortunately this happens:

> REV=test sed 's/{\\revision}{\(\w*\)}/\1'"$REV"'/' variables.tex

Why does this happen?

  • can u echo $REV and share the result
    – Siva
    Mar 13, 2020 at 9:18
  • That part works $ echo $REV 758d59ea Mar 13, 2020 at 9:26
  • 3
    Looks like it does what is programmed: eliminate the {s and }s, and the \revision string, then adds the test string.
    – RudiC
    Mar 13, 2020 at 9:53
  • Can you provide an example of the expected output? Mar 13, 2020 at 9:59

1 Answer 1


Try this,

sed -e "s/\({\\\revision}\){\w*}/\1{$REV}/" variables.tex

  • sed required two black slashes to escape backslash.
  • We should back-reference the {\\\revision} instead of {\w*}
  • finally required a curl brace around REV as per our expected output
  • 2
    This is it, I just placed the capture group in the wrong place. Without extended regex: sed 's/\({\\revision}{\)[^}]*/\1'"$REV"'/' Mar 13, 2020 at 10:53

You must log in to answer this question.

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