I have a .sed, .bash and .txt file.

The x.bash file has this within it


./y.sed "$1"

The z.txt file has this within it


The y.sed file has this command to find and replace <dstamp> with the current date

#!/bin/sed -rf
s/<dstamp>/$(date '+%Y%m%d')/g

This doesn't work. It substitutes <dstamp> with the literal $(date '+%Y%m%d')

Running it

./x.bash z.txt

output: $(date '+%Y%m%d')

However, the command works just fine in the terminal

sed -r "s/<dstamp>/$(date '+%Y%m%d')/g" z.txt

output: 20221014

How can I make this command work in my .sed file?

  • 1
    In this simple case, you could pass the substituted pattern to a shell (/bin/sh by default) using the e modifier ex. s/<dstamp>/date '+%Y%m%d'/e however it's tricky to apply to more complicated substitutions. Commented Oct 14, 2022 at 21:17
  • @steeldriver Thank you for the suggestion. It worked, but only when <dstamp> isn't preceded by anything. How could I make this global? To avoid writing this s/Date: <dstamp>/'+%Y%m%d'/e and simply write this s/<dstamp>/date '+%Y%m%d'/e ?
    – John
    Commented Oct 15, 2022 at 3:17
  • From bash, high level-ish, you call low level sed. Then sed can't do it and calls a shell :) That surely is a hint that you could solve your task directly using bash. Not clear though on the input file format. Does it contain only one line where <dstamp> is present somewhere?
    – seshoumara
    Commented Oct 15, 2022 at 6:19
  • @John that's exactly what I meant by "tricky" - in this case, you could capture the leading text and insert it into the date format string: s/(.*)<dstamp>/date '+\1%Y%m%d'/e Commented Oct 15, 2022 at 13:28

2 Answers 2


Try this. Assuming you have only below two lines in the y.sed file.

#!/usr/bin/sed -rf

s/<dstamp>/$(date '+%Y%m%d')/g

Updated the search string and replace string with variables in x.bash file. If you have any other data inside the y.sed file configure it in the echo statement in the last line in x.bash file. This is just a workaround.

Note: While executing this script sometimes not getting output. Please execute 4 to 5 times and check it.

Below is the x.bash file


time_data=$(date +'%Y%m%d')
echo "Updating variable values in y.sed script"
str="<dstamp>" date_value=${time_data} envsubst '$str $date_value' < y.sed | tee /root/scripts/y.sed
echo "Setting permission to y.sed file"
chmod 755 /root/scripts/y.sed
./y.sed "$1"
echo -e '#!/usr/bin/sed -rf \n\ns/$str/$date_value/g' > y.sed

Below is the y.sed file

#!/usr/bin/sed -rf


Below is the content in z.txt file

Test line 1
Test line 2 with <dstamp>
Test line <dstamp> and other data
<dstamp> in Test line 3
New line <dstamp> in line 4
Test line 5


Updating variable values in y.sed script
#!/usr/bin/sed -rf

Setting permission to y.sed file
Test line 1
Test line 2 with 20221015
Test line 20221015 and other data
20221015 in Test line 3
New line 20221015 in line 4
Test line 5

There is no need to create the sed script if you're already in a bash script.

sed 's/<dstamp>/'"$(date '+%Y%m%d')"'/g' "$1"

You must log in to answer this question.

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