One method is to use double quotes to allow the single quotes to be treated as normal characters. However, because shell variables are expanded within double quotes, the "$" needs to be escaped:
#!/usr/bin/env sh
# @(#) s1 Demonstrate insert of line with single quotes and variable.
set -o nounset
echo
debug=":"
debug="echo"
## Use local command version for the commands in this demonstration.
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash sed
echo
FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE
echo
echo " Results from sed:"
sed "
/permission/ a\
echo 'profile: /export/home/unix1/auto.pr' >> \$xxyy
" $FILE
exit 0
Producing:
% ./s1
(Versions displayed with local utility "version")
GNU bash 2.05b.0
GNU sed version 4.1.2
Input file data1:
This is a line
Another line
This line contains permission.
This is the line that should appear after the inserted line.
Last line.
Results from sed:
This is a line
Another line
This line contains permission.
echo 'profile: /export/home/unix1/auto.pr' >> $xxyy
This is the line that should appear after the inserted line.
Last line.
#!/bin/sh -
# @(#) s2 Demonstrate sed insertion with complex quoting.
FILE=${1-data1}
echo
echo " Input file $FILE:"
cat $FILE
echo
echo " Results from sed:"
sed '
/permission/ a\
echo '"'profile: /export/home/unix1/auto.pr'"' >> $xxyy' $FILE
exit 0
producing:
% ./s2
Input file data1:
This is a line
Another line
This line contains permission.
This is the line that should appear after the inserted line.
Last line.
Results from sed:
This is a line
Another line
This line contains permission.
echo 'profile: /export/home/unix1/auto.pr' >> $xxyy
This is the line that should appear after the inserted line.
Last line.
So it works correctly for me. That means there are two solutions that appear to work correctly.
Writing:
does not give us enough information.
Post the code you have tried and the results with copy and paste so that we can exactly what you have done, otherwise we're wasting time guessing what the problem might be ... cheers, drl