I am having trouble doing expansion with this because of the escape character.


Right now I am trying to stick with the really simple example of:

ps -ef | grep $USER

Eventually after I deal with the escape character I would like to do this.

ipcs -m | grep $USER | awk '{printf "%s ",$2}'

I know $USER has a value because I did this.

$ echo $USER UNF\1122

Please don't ask me why the administrator decided to put a \ in the username because I don't know.

To solve this I tried single quotes and double quotes. I have also tried to change username like this.

USER="UNF\\\\1122" and USER='UNF\\1122'


Tricky, as depending on the utility a \1 may have meaning, for example a back reference in grep, but not so in getent. Shell interpolation also complicates matters.

# getent sees a\\b, cannot find this literal string
$ getent passwd 'a\\b'

# let the shell interpolate \\ to \ so getent sees a\b
$ getent passwd a\\b
a\b:x:9999:9999:Slash Gordon:/:/bin/sh

# or no iterpolation, literal a\b passed to getent
$ getent passwd 'a\b'
a\b:x:9999:9999:Slash Gordon:/:/bin/sh

# oops, shell interpolated \\ to \ and thus grep sees \b metacharacter
$ grep "a\\b" /etc/passwd
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
a\b:x:9999:9999:Slash Gordon:/:/bin/sh

# oops, also shell interpolation, so grep sees a\b metachar
$ grep a\\b /etc/passwd
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
a\b:x:9999:9999:Slash Gordon:/:/bin/sh

# no interpolation, pass \\ to grep, which then treats as literal \
$ grep 'a\\b' /etc/passwd
a\b:x:9999:9999:Slash Gordon:/:/bin/sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.