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:

current_dir=$(pwd)
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!

Variables do not expand in single-quotes:

$ echo '$PWD'
$PWD

$ echo "$PWD"
/home/username

$

So put your sed expression in double quotes "" instead of single-quotes.

1 Like

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, a.sh and b.sh.

a.sh will make a copy of b.sh at /etc/init.d and will replace the "$PWD" keyword in the copy of b.sh.

Below is the edited code for my scripts:

a.sh

current_dir="$PWD"
echo $current_dir

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

b.sh

current_dir="$PWD"
echo $current_dir

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

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

please advise again...

To repeat:

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/copyOfb.sh
1 Like

yea.. i tried with

sed -i "s/$PWD/"$current_dir"/g" /etc/init.d/copyOfb.sh
before 

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/copyOfb.sh

but the content of copyOfb.sh remained as

current_dir="$PWD"

I was expecting

current_dir="/opt/xSystem"

if my a.sh is resided at /opt/xSystem directory.

please advise again.
Many thanks!

So "$PWD" is literal?

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

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

a.sh

mainlocation=$PWD
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!