LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 07-24-2004, 02:39 AM   #1
mpdavig
Member
 
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, 03:27 AM   #2
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Code:
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.
 
  


Reply


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 03:06 PM
Strange behavior with KDE tsw Linux - Software 2 05-09-2004 12:08 AM
Strange Behavior andrewb758 Linux - Hardware 5 08-31-2003 03:42 PM
strange behavior abhijit Linux - General 3 07-10-2003 12:25 AM
Strange DHCPcd behavior padukes Linux - Networking 0 03-17-2002 10:55 AM


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

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