Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Escape special characters in a string

0 views
Skip to first unread message

olli...@xfiles.com

unread,
Mar 17, 2005, 10:17:54 AM3/17/05
to
I'm hurting my brain trying to find a way to escape special characters
in a string entered by a user. The problem is that my echo is getting
screwed up by the special character before it has a chance to let sed
fix it. For example...

User enters string: "string

echo $@ | sed '/\"/\\"/g'

At runtime, this becomes:

echo "string | sed '/\"/\\"/g'

The shell now waits since there is no closing quote on the echo. I'm
stumped. Am I on the wrong track, or is this just not possible?

Ed Morton

unread,
Mar 17, 2005, 10:28:57 AM3/17/05
to

I assume your sed should have an "s" at the front and you don't need to
quote the " so the result would be:

echo $@ | sed 's/"/\\"/g'

I assume your user is calling the script as:

whatever "string

and that's what's hanging. If so, there's nothing you can do about that
within your script since the script won't even be invoked until the user
provides the terminating ". Try it with some non-existent script name
and you'll see what I mean. If the user wants to pass in a ", they'll
need to escape it, e.g.:

whatever \"string

and in that case your script will work.

Ed.

0 new messages