Skip to content
Advertisement

Sed is truncating first character if it is starting with “v” while appending in file

I am stuck in one of my production code where I am trying to append some content at the start of file and if the content which I want to append is starting with “v” , its getting truncated.

Steps to reproduce:

$ cat text.txt
Kubernetes document
$ var1=version1
$ sed -e "s/^/\${var1} /" text.txt

Output:

ersion1 Kubernetes document

Desired Output:

version1 Kubernetes document

Advertisement

Answer

Your use of the backslash is wrong. Check this:

var1=version1
echo "s/^/\${var1} /"

Your escaping leads to the following sed command:

s/^/version1 /

v is a special char: vertical tab, which is not visible on your terminal.

The simple solution is to not escape at all, it’s simply not needed when interpolating variables into a double quoted string:

sed -e "s/^/\${var1} /" text.txt
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement