LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 08: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, 10:11 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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 10:14 AM.
 
Old 07-29-2007, 10:20 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 12: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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:00 PM.

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
Open Source Consulting | Domain Registration