Download your favorite Linux distribution at LQ ISO.
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-29-2007, 09:57 AM   #1
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:

    dir=`echo $1 | sed 's/^~/$HOME/'`
    echo $dir

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

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

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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 at
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
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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

Original Poster
Rep: Reputation: 48
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.


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 > Forums > Non-*NIX Forums > Programming

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