Search code examples
linuxshellvariablesechouser-input

Shell script that inserts text to last line of a file based on user input


#!/bin/sh
...
read var
#user enters: it doesn't work
...
echo 'Some text and $var' > myfile.txt

Expected output:

cat myfile.txt
#Some text and it doesn't work

Actual output:

cat myfile.txt
#Some text and $var

So how to echo the content into the file with the value of the $var variable?


Solution

  • use double quote instead of simple quote to make variable remplacement available

    you can also remove your quotes to make it work but it's not recommanded

    http://wiki.bash-hackers.org/syntax/quoting