3

I cannot expand this variable in sed. I've tried everything I can think of.

I am trying to put the md5sum of file1 in line 10 of file2

I can take $x out of the regex and put some text and it works. It just will not accept the variable. printf the variable is fine.

#!/bin/bash
 x=$(md5sum /etc/file1)
 printf "$x \n"
 sed -i 10"s/.*/$x/g" /usr/bin/file2
WesZ
  • 129
  • 1
  • 10

2 Answers2

1

You may use this command that uses ~ as regex delimiter instead of / since output of md5sum contains /:

sed -i "10s~.*~$x~" /usr/bin/file2
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Hello, yes, same as I discovered... it was the output of md5sum that included /etc/file1 that was causing sed to choke. Thanks for your help. – WesZ Dec 17 '18 at 20:00
0

After I reduced the variable from the md5sum output which includes the filename and directory by running $x thru:

x=$(echo $x | head -n1 | awk '{print $1;}')

Leaving only the MD5 it worked and quit erroring.

WesZ
  • 129
  • 1
  • 10