Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-24-2004, 01:39 AM   #1
Registered: May 2004
Location: Boston, MA
Distribution: Fedora
Posts: 30

Rep: Reputation: 19
strange sed/bash behavior

I spent a couple of hours wrestling with this today and maybe someone else can make sense of this.

I've been doing shell scripting for a number of years and am fairly comfortable with escaping characters in regular expressions however, this one really threw me for a loop:

Why does this work:
> grep '^xinit' /usr/X11R6/bin/startx | sed -e 's/\$/\\\$/g'
xinit \$client \$clientargs -- \$server \$display \$serverargs
but this does not:
> xinit_line=`grep '^xinit' /usr/X11R6/bin/startx | sed -e 's/\$/\\\$/g'`; echo $xinit_line
xinit $client $clientargs -- $server $display $serverargs$

Tested this on a RedHat 7.3 (bash 2.05a sed 3.02) and Fedora Core 2 (bash 2.05b sed 4.0.8) systems.

What am I missing? I tried adding more backslashes but it would only affect the end of the string. Obviously I could use one method over the other but I always thought running the command directly or using `` was interchangeable.
Old 07-24-2004, 02:27 AM   #2
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
xinit_line=`grep '^xinit' /usr/X11R6/bin/startx | sed -e 's/\\$/\\\\\$/g'`; echo $xinit_line
This line is submitted to bash two times. In the first, when executing "xinit_line=..." it removes the apostrophe and pass "sed -e s/\$/\\\$/g" to the sub-shell called to do the substitution, requested by the grave accents. This sub-shell looks the sed and interprets it as "sed -d /$/\$/g", and pass it to sed process. Sed understands you want to append a dollar sign at the end of the line.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Very Strange Behavior raysr Mandriva 4 08-31-2004 02:06 PM
Strange behavior with KDE tsw Linux - Software 2 05-08-2004 11:08 PM
Strange Behavior andrewb758 Linux - Hardware 5 08-31-2003 02:42 PM
strange behavior abhijit Linux - General 3 07-09-2003 11:25 PM
Strange DHCPcd behavior padukes Linux - Networking 0 03-17-2002 09:55 AM

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration