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:
JavaScript
x
$ cat text.txt
Kubernetes document
$ var1=version1
$ sed -e "s/^/\${var1} /" text.txt
Output:
JavaScript
ersion1 Kubernetes document
Desired Output:
JavaScript
version1 Kubernetes document
Advertisement
Answer
Your use of the backslash is wrong. Check this:
JavaScript
var1=version1
echo "s/^/\${var1} /"
Your escaping leads to the following sed command:
JavaScript
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:
JavaScript
sed -e "s/^/\${var1} /" text.txt