LinuxQuestions.org
Review your favorite Linux distribution.
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-29-2007, 09:57 AM   #1
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
variables within sed substitutions?


Hi, I have yet another sed question. The following script unsuccessfully attempts to expand tildes within pathnames:
Code:
#!/bin/sh

expand()
{
    dir=`echo $1 | sed 's/^~/$HOME/'`
    echo $dir
}

expand '~'
expand '~/bin'
...with the output being:
Code:
$HOME
$HOME/bin
Whereas, the intended result is:
Code:
/home/username
/home/username/bin
How can the sed string substitution be corrected?

Thank you.
 
Old 07-29-2007, 11:11 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
See those single quotes?---To bash, they say: "don't attempt to interpret any special characters here--just pass them on to sed."

"$" has a different meaning to sed.

By using double-quotes, the "$" will be read by bash. (Then, to get back the sed meaning, you would need to use "\$")

So, the corrected line is:
dir=`echo $1 | sed "s/^~/$HOME/"`

Bash Guide for Beginners--by Machtelt Garrels....free at tldp.org
Also there: Advanced bash scripting guide by Mendel Cooper

Last edited by pixellany; 07-29-2007 at 11:14 AM.
 
Old 07-29-2007, 11:20 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Why don't simply let the bash to expand by itself (without using single quotes, as pixellany pointed out)?
Code:
#!/bin/bash
dir=`echo ~/bin`  # or simply: dir=~/bin
echo $dir
gives
Code:
/home/colucix/bin
 
Old 07-29-2007, 01:17 PM   #4
ocicat
Member
 
Registered: May 2007
Posts: 208

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by colucix
Why don't simply let the bash to expand by itself (without using single quotes, as pixellany pointed out)?
I'm using the Bourne shell, not Bash. pixellany is correct though about using double quotes instead of single (which I missed...). With double quotes string contents go through interpolation, meaning that the value of variables are substituted. This doesn't occur with single quotes. I'm guessing this has been part of shell programming from the beginning. Perl does the same thing, as it follows a lot of shell behavior given that originally Perl was meant to be an embellishment upon older shell themes.

Thanks to both for your answers.
 
  


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
Sed and variables oden Programming 9 07-29-2007 05:36 PM
sed and bash variables cashton2k Linux - Newbie 6 03-28-2007 12:09 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
sed? -- making substitutions scabies Linux - Software 1 12-22-2003 12:39 AM
Case sensitivity in SED substitutions jkoch Programming 1 02-21-2003 05:15 PM


All times are GMT -5. The time now is 04:03 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