0

I need your help. I am creating bash script and I have problem in part where I need to add text in line 4 of test.txt file. My text is including variable. I know how to add text with variable, but in this case this variable must be in double quotation.

So, I am using this command:

sed -i "4s/$/Username = "$var1"/g" $dir/output/test.txt

but I get next results in test.txt file:

$Username = example

I tried many options but I can not achive to get variable in double quotation:

$Username = "example"
jww
  • 97,681
  • 90
  • 411
  • 885
  • See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Dec 09 '19 at 20:59
  • Use `sed -i '4s/$/Username = "'"$var1"'"/g' $dir/output/test.txt` – Wiktor Stribiżew Dec 09 '19 at 21:09
  • 1
    A duplicate of [How to escape single quotes within single quoted strings](https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings) – Wiktor Stribiżew Dec 09 '19 at 21:10
  • @WiktorStribiżew it works. Thank you so much. – jusk pikanet Dec 09 '19 at 21:15

1 Answers1

0
sed -i "4s/$/Username = \"$var1\"/" $dir/output/test.txt

Global switch is meaningless and can be removed since you're appending to the end of the line

Kyle Banerjee
  • 2,554
  • 4
  • 22
  • 30