0

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

2
  • 1
    sed -i "\$a gem \'forum2discourse\'" file, or just echo "gem 'forum2discourse'" >> file...
    – jasonwryan
    May 17, 2015 at 9:22
  • or try sed -i '$a gem '"'forum2discourse'" Gemfile
    – Skaperen
    May 17, 2015 at 10:06

3 Answers 3

4

sed -i '$a gem '"'"'forum2discourse'"'" Gemfile

Alternate Solution

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

1

At least with GNU sed:

sed -i.bak '$a gem \x27forum2discourse\x27' file
0
sed -i -e '$a gem '"'"'forum2discourse'"'" yourfile
2
  • No single quotes, as specified in the question...
    – jasonwryan
    May 17, 2015 at 9:31
  • has been updated.... May 17, 2015 at 9:46

Not the answer you're looking for? Browse other questions tagged .