This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 7 years ago.
#! /bin/bash
A=$1
B=$2
i= ... # i is a filename generated with find command
j="$(echo $i | sed -E 's/$A/$B/')"
I am trying to change part of i, (A in this case), into something else (B in this case), and store it in j.
I've executed this, but apparently j is identical to i. I'm pretty sure I've put command line arguments correctly.
Did I do something wrong?
You need to replace single quotes with double quotes in 's/$A/$B/'. The following command should work.
j=$( echo $i | sed -E "s/$A/$B/" )
Related
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
Basically, I want to send a variable as $1 in another script without the value it has saved.
#!/bin/bash
echo -e "#!/bin/bash\ncp ~/src/$1" > ~/asset/newfile.sh
So, that in the file newfile.sh it is written:
#!/bin/bash
cp ~/src/$1
You can escape the dollar sign with a backslash:
echo -e "#!/bin/bash\ncp ~/src/\$1"
Or, switch to single quotes:
echo -e '#!/bin/bash\ncp ~/src/$1'
This question already has answers here:
Linux sed command - using variable with backslash
(2 answers)
Closed 1 year ago.
I'm trying to replace all \ with \\ in bash, I'm doing it like this but bash gets stuck in a never-ending loop. Where am I going wrong?
myVar="${myVar//\//\\\\}"
You can use sed for that:
echo "hello\world\hello\world" | sed 's/\\/\\\\/g'
Outputs:
hello\\world\\hello\\world
This question already has answers here:
Replace one substring for another string in shell script
(16 answers)
Closed 4 years ago.
I've been trying to wrap my head around this for over an hour now and my searches haven't helped yield the answer.
Trying to set a variable inside a bash script. This variable is taking variable-A and removing variable-B from it.
Prefix="$(echo ${Process} | sed -e 's/${Server}//g')"
So if Process=abcd1wxyz01 and Server=wxyz01, then Prefix should end up being abcd1.
I've tried so many iterations from online searches I honestly can't recall what all I've tried.
Your problem are the quotes, as pointed out in afsal_p's answer.
You could do this with parameter expansion instead:
$ process=abcd1wxyz01
$ server=wxyz01
$ prefix=${process%"$server"}
$ echo "$prefix"
abcd1
The ${word%suffix} expansion removes suffix from the end of word.
please use " instead of ' while using bash variables inside sed:
Prefix="$(echo ${Process} | sed -e "s/${Server}//g")"
echo $Prefix
This question already has answers here:
Environment variable substitution in sed
(12 answers)
SED not working [unterminated `s' command]
(3 answers)
Closed 7 years ago.
I have a variable called "num", that holds a number. I want to use it in "sed" like so:
sed '1,$(num)d' file.txt
also tried:
sed '1,($num)d' file.txt
...so that I can delete lines from 1 until the line num.
It gives me an error like this:
sed: -e expression #1, char 4: unknown command: `('
What is the correct way to do this? thank.
Your shell variable usage was incorrect. First, using the double quote ensures that the shell will expand the variable. Second, surrounding the variable in question (num) with the braces ensures that the variable will be seen by the shell as $num, instead of the subsequent d getting glommed on.
Here is how you should specify what you want to do:
sed "1,${num}d" file.txt
You can use single quotes but need to concatenate the script
num=42; seq 1 45 | sed '1,'$num'd'
will print
43
44
45
as expected.
This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 6 years ago.
I execute the following bash script:
#!/bin/bash
version=$1
echo $version
sed 's/\${version.number}/$version/' template.txt > readme.txt
I'm expecting to replace all instances of ${version.number} with the contents of the variable "version". Instead the literal text $version is being inserted.
What do I need to do to make sed use the current value of $version instead?
sed "s/\${version.number}/$version/" template.txt > readme.txt
Only double quotes do dollar-sign replacement. That also means single quotes don't require the dollar sign to be escaped.
You could also simply unquote the variables
sed 's/'${version.number}'/'$version'/' template.txt > readme.txt
It's a bit old, but it might still be helpful...
For me it worked using a double escape as Philip said (and escaping parenthesis, if you use them...):
#!/bin/bash
LIVE_DB_NAME='wordpress';
STAGING='staging';
sed -r "s/define\('DB_NAME', '[a-zA-Z0-9]+'\);/define('DB_NAME', '\\${LIVE_DB_NAME}');/" ${STAGING}/wp-config.php >tmp1;