'escape " double quotes" and slash (/) while variable substitution in sed
I want to escape "" and / for my VAR. InFile contains below variable
var_value='"skdskdlskdlskjdlsdjsld/jshdks00=="'
Echo ${var_value}
"skdskdlskdlskjdlsdjsld/jshdks00=="
While substitution i do not want / and "" I tried
sed "s#JWT=<<CHANGE_ME>>#"${JWT}"#g" InFile > OutFile
Expected OutFile :
JWT=skdskdlskdlskjdlsdjsld/jshdks00==
A help here would be much appreciated
Solution 1:[1]
Use
sed 's#JWT=<<CHANGE_ME>>#JWT='"${JWT}"'#g' InFile > OutFile
Here, the concatenation scheme is the following:
's#JWT=<<CHANGE_ME>>#JWT='"${JWT}"'#g'
|------------------------||------||--|
1 2 3
1- single quoted part2- a double quoted string with interpolated variable3- single quoted part again.
See the online demo:
#!/bin/bash
JWT="skdskdlskdlskjdlsdjsld/jshdks00=="
sed 's#JWT=<<CHANGE_ME>>#JWT='"${JWT}"'#g' <<< "JWT=<<CHANGE_ME>>"
## => JWT=skdskdlskdlskjdlsdjsld/jshdks00==
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wiktor Stribiżew |
