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
Related
This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
I have this string in my file
$vnet_address
And i have in my bash run time this variable which i want to replace my string with:
$vnet_address=10.1.0.0/16
I get an error when i run this command:
sed -i "/\$vnet_address/ s//$vnet_address/"
And i believe it is because of the slash character '/' in my variable.
How to go about it?
You can use
sed -i "s,\\\$vnet_address,${vnet_address}," file
Note:
The regex delimiter character is replaced with ,
The $ is properly escaped with a literal \ character
See the online demo:
#!/bin/bash
s='$vnet_address'
vnet_address=10.1.0.0/16
sed "s,\\\$vnet_address,${vnet_address}," <<< "$s"
# => 10.1.0.0/16
This question already has answers here:
Use sed to replace all backslashes with forward slashes
(9 answers)
Closed 3 years ago.
How do I replace the special character \ (Backslash) to - (Dash) in bash script.
I have tried sed but didn't work.
whoami
example - when I run whoami the output is DomainName\User
Current output - DomainName\User
Expected output - DomainName-User
I reckon the escape character is missing
root#:~# echo "google\account" | sed 's/\\/-/'
google-account
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:
Use a variable's value in a sed command [duplicate]
(6 answers)
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
i am just trying to append a line from my bash script:
sed -i '$ a TEXT' "filename"
but when TEXT is a variable it does not work
sed -i '$ a $VARIABLE' "filename"
i have tested endless iterations with ',''," before/after $ and ${}, escape with \ etc., but i always get a literal $VARIABLE in my file
ps. please do not propose to use echo:)
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;