Reputation: 3689
in a bash script file, I have set one variable like this:
current_path=`pwd`
sed -i "1s/.*/working_path='$current_path';/" file1.sh
I want to run this script to replace the first line of file1.sh
into working_path='$current_path';
, but the current_path
has the /
and in the sed
command, the /
is predefined in sed
replace pattern.
And I have tried this:
current_path1="${current_path/\//\\\/}"
the above line, I want to replace the /
in variable current_path
into \/
, then input the current_path1
into the sed
command, but also has an error.
Could you give me some advice, please? Thanks.
Upvotes: 4
Views: 9954
Reputation: 21
Please add a '/' to the beginning of pattern string. It replaces all matches of pattern with string.
current_path1="${current_path//\//\\\/}"
Upvotes: 0
Reputation: 246744
You can use different delimiters for the s///
command:
current_path=`pwd`
sed -i "1s|.*|working_path='$current_path';|" file1.sh
But you're not really searching and replacing here,, you want to insert the new line and delete the old line:
current_path=`pwd`
sed -i -e "1i\working_path='$current_path)';" -e 1d file1.sh
Are you really changing the first line of a .sh
file? Are you deleting the she-bang line?
Upvotes: 2
Reputation: 10522
Try this.
sed -i -e "1s@.*@working_path='$current_path';@" file1.sh
Use @
instead of /
in the substitute command.
Upvotes: 7