LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   sed exclamation (http://www.linuxquestions.org/questions/linux-general-1/sed-exclamation-828353/)

Moshanator 08-25-2010 04:56 AM

sed exclamation
 
Hi,
trying to automatically disable some DAEMONS in /etc/rc.conf. This requires inserting ecxlamation signs itno the file.
The example command
Code:

echo Hi?|sed 's/?/!/'
returns "unterminated `s' command".
but
Code:

echo netfs|sed 's/netfs/!netfs/'
returns !netfs as expected.
Where does the difference come from?

konsolebox 08-25-2010 05:18 AM

Odd. Try this one:
Code:

echo 'Hi?' | sed 's/?/\!/'

giammy 08-25-2010 05:25 AM

Quote:

Originally Posted by Moshanator (Post 4077033)
Hi,
trying to automatically disable some DAEMONS in /etc/rc.conf. This requires inserting ecxlamation signs itno the file.
The example command
Code:

echo Hi?|sed 's/?/!/'
returns "unterminated `s' command".
but
Code:

echo netfs|sed 's/netfs/!netfs/'
returns !netfs as expected.
Where does the difference come from?

Hi,

I suppose the difference is that the ? is probably interpreted as a specia character
by the shell.
In my system, Mac OS X 10.6, with the default shell (Terminal.app) I
correctly get:

Code:

~ $ echo Hi?|sed 's/?/!/'
Hi!

try escaping the "?"

bye
giammy

Moshanator 08-25-2010 08:20 AM

Quote:

Originally Posted by konsolebox (Post 4077045)
Odd. Try this one:
Code:

echo 'Hi?' | sed 's/?/\!/'

Code:

echo 'Hi?' | sed 's/?/\!/'
Hi!

Very strange. Why would I need to escape the ! in some cases but not others?
Which is why I checked my bash history and discovered this:
Code:

echo Hi?|sed 's/?/!'
Silly me, the last / was missing all along. Should've checked that in the first place.
Let's hope you won't have to see more questions like this one. I really appreciate your help.

David the H. 08-25-2010 09:08 AM

It may not be the problem you're experiencing, but it should be pointed out that ! is generally seen as a special character in interactive shells. It has the function of starting a history expansion, and if followed by a number or string that references a previous command, it will insert it into the current command at that location. It will also likely spit out an error if followed by something unrecognized as one.

This even works inside of double quotes, but single quotes or the backslash will escape it.

Try this, for example:
Code:

echo "Hi?" | sed "s/?/!!/"
!! expands to the previous command entered, so you'll probably get something like Himplayer pr0n.avi as a result. ;)

Note though that this isn't usually a problem in scripts, only the command line. See the HISTORY EXPANSION section in the bash man page for details.


All times are GMT -5. The time now is 07:01 AM.