LinuxQuestions.org
Review your favorite Linux distribution.
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 05-22-2005, 10:28 AM   #1
chii-chan
Member
 
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357

Rep: Reputation: 30
sed doesn't accept $variable in bash script


I made this script to replace text using 'sed':

Code:
#!/bin/bash

txt_ori="original text"
txt_rplc="replacement text"

for text in "$@"
do
    sed 's/$txt_ori/$txt_rplc/g' $text > text.out
done
But 'sed' doesn't seem to accept the variable. Anyway to make 'sed' to read the variables?
 
Old 05-22-2005, 10:40 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Re: sed doesn't accept $variable in bash script

Quote:
Originally posted by chii-chan
I made this script to replace text using 'sed':

Code:
#!/bin/bash

txt_ori="original text"
txt_rplc="replacement text"

for text in "$@"
do
    sed 's/$txt_ori/$txt_rplc/g' $text > text.out
done
But 'sed' doesn't seem to accept the variable. Anyway to make 'sed' to read the variables?
You are passing two arguments to sed. The first is the literal string s/$txt_ori/$txt_rplc/g, the second is the value of $text

I assume that the value it's not accepting is $txt_ori or $txt_rplc (you can find out exactly what you are passing by using echo instead of sed).

BASH treats anything in single-quotes as a literal string, and will not try to interpolate any variables. You probably want to be using double-quotes instead.
 
Old 05-22-2005, 11:21 AM   #3
chii-chan
Member
 
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357

Original Poster
Rep: Reputation: 30
Thanks for noticing me the single quotes. It seems that 'sed' needs this kind of line:

Code:
sed -e 's/SEARCH_STRING/REPLACE_STRING/g' file > file.out
Using double-quotes i.e.:

Code:
sed -e "s/"$txt_ori"/"$txt_rplc"/g" $text > text.out
doesnt't work. But instead I did this on try and error:

Code:
sed -e s/"$txt_ori"/"$txt_rplc"/g $text > text.out
then it works.
 
Old 05-22-2005, 12:26 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Or use eval bash function like :
Code:
eval "sed -e s/$txt_ori/$txt_rplc/g $text > text.out"
This way you just quote at the beginning and at the end of
the sed line, not inside
 
Old 05-22-2005, 01:38 PM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Re: sed doesn't accept $variable in bash script

Quote:
Originally posted by chii-chan
I made this script to replace text using 'sed':

Code:
#!/bin/bash

txt_ori="original text"
txt_rplc="replacement text"

for text in "$@"
do
    sed 's/$txt_ori/$txt_rplc/g' $text > text.out
done
But 'sed' doesn't seem to accept the variable. Anyway to make 'sed' to read the variables?
All you need to do (apart from learning how to treat metacharacters in the shell ) is use double quotes instead of the single ones, so the $ variables are expanded:
Code:
    sed "s/$txt_ori/$txt_rplc/g" $text > text.out
 
Old 05-25-2005, 07:33 AM   #6
chii-chan
Member
 
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357

Original Poster
Rep: Reputation: 30
Thanks for the suggestions. So far the only working one is:

Code:
sed -e s/"$txt_ori"/"$txt_rplc"/g $text > text.out
Code:
sed -e "s/$txt_ori/$txt_rplc/g" $text > text.out
I can't get these to work though:

Code:
eval "sed -e s/$txt_ori/$txt_rplc/g $text > text.out"
But, really, thanks for all of the suggestions since I can apply that to other scripts later.

Last edited by chii-chan; 05-25-2005 at 07:49 AM.
 
Old 05-28-2005, 07:07 AM   #7
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Quote:
Originally posted by chii-chan
I can't get these to work though:

Code:
eval "sed -e s/$txt_ori/$txt_rplc/g $text > text.out"
This may or may not work, depending on the contents of the values of the variables you are quoting.

If any of them contains a space, this will create a new parameter, which probably isn't what you want. If they contain <chevrons>, then you will start a file redirect. If they contain a pipe (|) then you will start a file redirect.

If you run
Code:
man bash
then you can read all about how and when BASH copes with special characters. For example:
Quote:
eval [arg ...]
The args are read and concatenated together into a single command. This command is then read and exe_cuted by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0.
 
  


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
Bash, using variable in sed fur Programming 3 11-12-2005 07:41 AM
problem with sed in a bash script nexus55 Linux - Software 6 05-03-2004 09:40 PM
bash script to accept input ONLY until background process completes andrewstr Linux - Software 2 03-17-2004 12:02 PM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM

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

All times are GMT -5. The time now is 02:30 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
Open Source Consulting | Domain Registration