I want to append the following line to a text file with sed:
gem 'forum2discourse'
I've tried sed -i '$a gem \'forum2discourse\'' Gemfile
but this drops me to a >
prompt so I think I must be incorrectly escaping the '
characters
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communitysed -i '$a gem '"'"'forum2discourse'"'" Gemfile
If you wish to do it your way, then use the bash $'string'
format. Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
sed -i $'$a gem \'forum2discourse\'' Gemfile
Source: http://wiki.bash-hackers.org/syntax/quoting#ansi_c_like_strings
sed -i -e '$a gem '"'"'forum2discourse'"'" yourfile
sed -i "\$a gem \'forum2discourse\'" file
, or justecho "gem 'forum2discourse'" >> file
...sed -i '$a gem '"'forum2discourse'" Gemfile