I found interesting discussion here and tried the solution given with sed
How to insert a text at the beginning of a file?
It works with normal text, but not when the text is saved in a variable.
wolf@linux:~$ cat file.txt
1
2
3
wolf@linux:~$
Add text (<added text>
) to the beginning of the file
wolf@linux:~$ sed -i '1s/^/<added text>\n/' file.txt
wolf@linux:~$
It works!
wolf@linux:~$ cat file.txt
<added text>
1
2
3
wolf@linux:~$
However, when I tried it with variable it doesn't work anymore.
wolf@linux:~$ var='Text 1'
wolf@linux:~$ echo $var
Text 1
wolf@linux:~$
wolf@linux:~$ cat file.txt
1
2
3
wolf@linux:~$ sed -i '1s/^/"$var"\n/' file.txt
wolf@linux:~$ cat file.txt
"$var"
1
2
3
wolf@linux:~$
Instead of "$var"
, how do I call the actual value of $var
in sed
command? I use GNU sed
4.7:
sed (GNU sed) 4.7 Packaged by Debian
Copyright (C) 2018 Free Software Foundation, Inc.