How to pass value of pwd as variable in SED to replace variable in a script file

Thread Tools Search this Thread
Operating Systems Linux Red Hat How to pass value of pwd as variable in SED to replace variable in a script file
# 1  
Old 03-27-2012
How to pass value of pwd as variable in SED to replace variable in a script file

Hi all,

Hereby wish to have your advise for below:

Main concept is
I intend to get current directory of my script file.
This script file will be copied to /etc/init.d.
A string in this copy will be replaced with current directory value.

Below is original script file:

cp $current _dir/myScript /etc/init.d/myService
sed -i 's/$(pwd)/$current_dir/g' /etc/init.d/myService

I keep on got error on passing $pwd to sed... please advise...

Many thanks!
# 2  
Old 03-27-2012
Variables do not expand in single-quotes:

$ echo '$PWD'

$ echo "$PWD"


So put your sed expression in double quotes "" instead of single-quotes.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-27-2012
Hi Corona688, thank you for your prompt reply.

I have tested and tried to edit my script with yout advice.. but still unable to get the value of PWD to replace my copy of shell script.

I have 2 shell script, and will make a copy of at /etc/init.d and will replace the "$PWD" keyword in the copy of

Below is the edited code for my scripts:

echo $current_dir

cp $current_dir/ /etc/init.d/
sed -i 's/$PWD/"$current_dir"/g' /etc/init.d/
echo $current_dir

when i execute the the keyword PWD in copy of is replaced with the string "$current_dir"
and eventually i have this
in my copyOf

I tried to manipulate the quotes.. but still unable to get the thing right...

please advise again...
# 4  
Old 03-27-2012
To repeat:

Originally Posted by Corona688
Variables do not expand in single-quotes:
Change the quotes to double quotes. You can escape the inner double quotes with \.

It's also not great to use / as the what-dya-call-it when dealing with paths.

sed -i "s|$PWD|\"$current_dir\"|g" /etc/init.d/

This User Gave Thanks to Scott For This Post:
# 5  
Old 03-27-2012
yea.. i tried with
[CODE]sed -i "s/$PWD/"$current_dir"/g" /etc/init.d/

but i got this error message:

sed: -e expression #1, char 9: unknown option to `s'

Then i tried with your advise,
sed -i "s|$PWD|\"$current_dir\"|g" /etc/init.d/

but the content of remained as

I was expecting

if my is resided at /opt/xSystem directory.

please advise again.
Many thanks!
# 6  
Old 03-27-2012
So "$PWD" is literal?

sed -i "s|\$PWD|....

This User Gave Thanks to Scott For This Post:
# 7  
Old 03-28-2012
thanks Scott!

It is working after i reply my code with your advice.

May i know what is the syntax "\" and "|" for?
What is all this syntax called? as i need to get appropriate keyword to Google around.

May i have another advise again?

How can i assign another variable to the higher directory of my current directory?

Currently i am doing this in stupid way:

higher_dir=$(cd .. pwd)

please advice again..

---------- Post updated 03-28-12 at 01:47 AM ---------- Previous update was 03-27-12 at 09:33 PM ----------

i intend to get the upper level of a location that i will .
I passed this location to a variable
upperlevel = $(cd .. $mainlocation)

saying if my $PWD is eventually is assigned as "/opt/xSystem/config" ..
how can i assign "/opt/xSystem" to upperlevel

please advise.

Many thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed to replace variable in file

Hi All I have one file with multiple lines in it, each line has static text and some variable enclosed in <<filename>> as well. e.g. as below 123, <<file1.txt>> this is my name, I stay at <<city.txt>> Thanks for visiting 348384y, this is my name <<fileabc.txt>>, I stay at near the mall of... (8 Replies)
Discussion started by: reldb
8 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

ksh script trying to pass a variable to edit a file

I'm trying to create a ksh script that will ask the user for the port number. $PORT1 is the variable I want to use that will contain whatever numbers the user inputs. The script would edit ports.txt file, search and delete "./serv$PORT1 200;=3" . So if the user types 50243 then the... (5 Replies)
Discussion started by: seekryts15
5 Replies

4. Shell Programming and Scripting

How to replace a string with a variable in a file using sed?

I have a file having some text like: PATH_ABC=/user/myLocation I have to replace "/user/myLocation" with a session variable say, $REPLACE_PATH, where $REPLACE_PATH=/user/myReplaceLocation The following sed command is not working. It is writing PATH_ABC=$REPLACE_PATH in the file ... (2 Replies)
Discussion started by: SKhan
2 Replies

5. Shell Programming and Scripting

Replace value of a variable in a file through script using sed

Hi, I am trying to replace the value of a variable in a file through another script. Example: Filename : abc.txt contents: a=10 b=20 c=30 Need to change the value of, say, b - Tried using the following: sed "s/${b}/15/g" abc.txt Have tried various forms of sed (with single quotes,... (4 Replies)
Discussion started by: rituparna_gupta
4 Replies

6. Shell Programming and Scripting

using file-and-replace sed command with the use of a variable?

Ok, so, let's say I have the variable $GMAILID....How can I use it with sed command so to replace a string in a file? e.g.: sed -i 's/$GMAILID/' /home/$USER/Desktop/sendmail (4 Replies)
Discussion started by: hakermania
4 Replies

7. Shell Programming and Scripting

pass variable to sed like in awk (-v switch)

hi all is possible to pass shell (bash) variable to sed like it is in awk? example: awk script is storred in awk.awk file and I am passing variable called var to this file. $ cat awk.awk {if ($5==var) print $0} so it works when i issue $ bash_var=24 $ ls -l | awk -v... (1 Reply)
Discussion started by: wakatana
1 Replies

8. Shell Programming and Scripting

Pass perl variable to sed

Hello, I'd like to pass a variable to a sed command in a perl script. The script is like this : #!/usr/bin/perl -w $newline="new"; system q(sed '/insert/ i\ '$newline <sed1.txt >sed2.txt); But the interpretor wouldn't recognize $newline, it inserts a "\n" instead. I've also... (4 Replies)
Discussion started by: hi_ryo
4 Replies

9. Shell Programming and Scripting

pass a variable to sed p in a loop?

Hi, Thanks for looking,,,, (kornshell) tmp3 is a list of line numbers I want to print the lines from my list code: while read j do echo $j #works fine echo $filename #works fine #sed "'$jp'" "$filename" ... (7 Replies)
Discussion started by: JohnMario
7 Replies

10. Shell Programming and Scripting

Pass variable to sed?

Hi there. If variables are named inside of a ksh script, how is it possible to pass these to sed? The affected portion of my script goes something like this: A=`cut -d. -f1 $FILE` B=`cut -d. -f2 $FILE` C=`cut -d. -f3 $FILE` sed 's/1111/$A/g;s/2222/$B/g;s/3333/$C/g' file > anotherfile ... (2 Replies)
Discussion started by: kristy
2 Replies
Login or Register to Ask a Question

Featured Tech Videos