How do I use variables in a sed command?











up vote
199
down vote

favorite
54












I tried the following code to replace QQ with ZZ, but it doesn't do what I want:



var1=QQ
sed -i 's/$var1/ZZ/g' $file


However, this code does what I want:



sed -i 's/QQ/ZZ/g' $file


How do I use variables in sed?










share|improve this question
























  • see my answer in this post of yours
    – ata
    Nov 7 '11 at 16:54












  • related: stackoverflow.com/questions/407523/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Mar 28 at 12:43















up vote
199
down vote

favorite
54












I tried the following code to replace QQ with ZZ, but it doesn't do what I want:



var1=QQ
sed -i 's/$var1/ZZ/g' $file


However, this code does what I want:



sed -i 's/QQ/ZZ/g' $file


How do I use variables in sed?










share|improve this question
























  • see my answer in this post of yours
    – ata
    Nov 7 '11 at 16:54












  • related: stackoverflow.com/questions/407523/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Mar 28 at 12:43













up vote
199
down vote

favorite
54









up vote
199
down vote

favorite
54






54





I tried the following code to replace QQ with ZZ, but it doesn't do what I want:



var1=QQ
sed -i 's/$var1/ZZ/g' $file


However, this code does what I want:



sed -i 's/QQ/ZZ/g' $file


How do I use variables in sed?










share|improve this question















I tried the following code to replace QQ with ZZ, but it doesn't do what I want:



var1=QQ
sed -i 's/$var1/ZZ/g' $file


However, this code does what I want:



sed -i 's/QQ/ZZ/g' $file


How do I use variables in sed?







command-line bash sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 14 '17 at 3:01









wjandrea

8,05142258




8,05142258










asked Nov 7 '11 at 16:30









UAdapter

5,062346593




5,062346593












  • see my answer in this post of yours
    – ata
    Nov 7 '11 at 16:54












  • related: stackoverflow.com/questions/407523/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Mar 28 at 12:43


















  • see my answer in this post of yours
    – ata
    Nov 7 '11 at 16:54












  • related: stackoverflow.com/questions/407523/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Mar 28 at 12:43
















see my answer in this post of yours
– ata
Nov 7 '11 at 16:54






see my answer in this post of yours
– ata
Nov 7 '11 at 16:54














related: stackoverflow.com/questions/407523/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Mar 28 at 12:43




related: stackoverflow.com/questions/407523/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Mar 28 at 12:43










2 Answers
2






active

oldest

votes

















up vote
275
down vote



accepted










The shell is responsible for expanding variables. When you use single quotes for strings, its contents will be treated literally, so sed now tries to replace every occurrence of the literal $var1 by ZZ.



Using double quotes



Use double quotes to make the shell expand variables while preserving whitespace:



sed -i "s/$var1/ZZ/g" "$file"


When you require the quote character in the replacement string, you have to precede it with a backslash which will be interpreted by the shell. In the following example, the string quote me will be replaced by "quote me" (the character & is interpreted by sed):



sed -i "s/quote me/"&"/" "$file"


Using single quotes



If you've a lot shell meta-characters, consider using single quotes for the pattern, and double quotes for the variable:



sed -i 's,'"$pattern"',Say hurrah to &: /,' "$file"


Notice how I use s,pattern,replacement, instead of s/pattern/replacement/, I did it to avoid interference with the / in /.



Example



The shell then runs the above command sed with the next arguments (assuming pattern=bert and file=text.txt):



-i
s,bert,Say hurrah to &: /,
text.txt


If file.txt contains bert, the output will be:



Say hurrah to bert: /





share|improve this answer



















  • 1




    How would one pass the "/g" option using this comma separated form?
    – blong
    Apr 1 '14 at 13:55






  • 5




    @b.long It's a g option, so you would pass s,foo,bar,g instead.
    – Lekensteyn
    Apr 1 '14 at 14:32






  • 1




    your single quote example isparticularly useful, covers sed using regex pattern pretty nicely!
    – Brian Thomas
    Sep 9 '16 at 1:08












  • In case you are tempted to use \0 instead of , it should not be done when is enclosed in single quotes. Otherwise sed will substitute the pattern for a literal instead of the whole match.
    – Lekensteyn
    Aug 23 at 17:20


















up vote
85
down vote













We can use variables in sed using double quotes:



sed -i "s/$var/r_str/g" file_name


If you have a slash / in the variable then use different separator, like below:



sed -i "s|$var|r_str|g" file_name





share|improve this answer



















  • 1




    If you have a slash / in the variable => This saved me ! My variable is a url and it contains /. Switching to use | as separator fixed my issue
    – sonlexqt
    Sep 20 at 4:39












protected by Community Nov 30 at 16:21



Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



Would you like to answer one of these unanswered questions instead?














2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
275
down vote



accepted










The shell is responsible for expanding variables. When you use single quotes for strings, its contents will be treated literally, so sed now tries to replace every occurrence of the literal $var1 by ZZ.



Using double quotes



Use double quotes to make the shell expand variables while preserving whitespace:



sed -i "s/$var1/ZZ/g" "$file"


When you require the quote character in the replacement string, you have to precede it with a backslash which will be interpreted by the shell. In the following example, the string quote me will be replaced by "quote me" (the character & is interpreted by sed):



sed -i "s/quote me/"&"/" "$file"


Using single quotes



If you've a lot shell meta-characters, consider using single quotes for the pattern, and double quotes for the variable:



sed -i 's,'"$pattern"',Say hurrah to &: /,' "$file"


Notice how I use s,pattern,replacement, instead of s/pattern/replacement/, I did it to avoid interference with the / in /.



Example



The shell then runs the above command sed with the next arguments (assuming pattern=bert and file=text.txt):



-i
s,bert,Say hurrah to &: /,
text.txt


If file.txt contains bert, the output will be:



Say hurrah to bert: /





share|improve this answer



















  • 1




    How would one pass the "/g" option using this comma separated form?
    – blong
    Apr 1 '14 at 13:55






  • 5




    @b.long It's a g option, so you would pass s,foo,bar,g instead.
    – Lekensteyn
    Apr 1 '14 at 14:32






  • 1




    your single quote example isparticularly useful, covers sed using regex pattern pretty nicely!
    – Brian Thomas
    Sep 9 '16 at 1:08












  • In case you are tempted to use \0 instead of , it should not be done when is enclosed in single quotes. Otherwise sed will substitute the pattern for a literal instead of the whole match.
    – Lekensteyn
    Aug 23 at 17:20















up vote
275
down vote



accepted










The shell is responsible for expanding variables. When you use single quotes for strings, its contents will be treated literally, so sed now tries to replace every occurrence of the literal $var1 by ZZ.



Using double quotes



Use double quotes to make the shell expand variables while preserving whitespace:



sed -i "s/$var1/ZZ/g" "$file"


When you require the quote character in the replacement string, you have to precede it with a backslash which will be interpreted by the shell. In the following example, the string quote me will be replaced by "quote me" (the character & is interpreted by sed):



sed -i "s/quote me/"&"/" "$file"


Using single quotes



If you've a lot shell meta-characters, consider using single quotes for the pattern, and double quotes for the variable:



sed -i 's,'"$pattern"',Say hurrah to &: /,' "$file"


Notice how I use s,pattern,replacement, instead of s/pattern/replacement/, I did it to avoid interference with the / in /.



Example



The shell then runs the above command sed with the next arguments (assuming pattern=bert and file=text.txt):



-i
s,bert,Say hurrah to &: /,
text.txt


If file.txt contains bert, the output will be:



Say hurrah to bert: /





share|improve this answer



















  • 1




    How would one pass the "/g" option using this comma separated form?
    – blong
    Apr 1 '14 at 13:55






  • 5




    @b.long It's a g option, so you would pass s,foo,bar,g instead.
    – Lekensteyn
    Apr 1 '14 at 14:32






  • 1




    your single quote example isparticularly useful, covers sed using regex pattern pretty nicely!
    – Brian Thomas
    Sep 9 '16 at 1:08












  • In case you are tempted to use \0 instead of , it should not be done when is enclosed in single quotes. Otherwise sed will substitute the pattern for a literal instead of the whole match.
    – Lekensteyn
    Aug 23 at 17:20













up vote
275
down vote



accepted







up vote
275
down vote



accepted






The shell is responsible for expanding variables. When you use single quotes for strings, its contents will be treated literally, so sed now tries to replace every occurrence of the literal $var1 by ZZ.



Using double quotes



Use double quotes to make the shell expand variables while preserving whitespace:



sed -i "s/$var1/ZZ/g" "$file"


When you require the quote character in the replacement string, you have to precede it with a backslash which will be interpreted by the shell. In the following example, the string quote me will be replaced by "quote me" (the character & is interpreted by sed):



sed -i "s/quote me/"&"/" "$file"


Using single quotes



If you've a lot shell meta-characters, consider using single quotes for the pattern, and double quotes for the variable:



sed -i 's,'"$pattern"',Say hurrah to &: /,' "$file"


Notice how I use s,pattern,replacement, instead of s/pattern/replacement/, I did it to avoid interference with the / in /.



Example



The shell then runs the above command sed with the next arguments (assuming pattern=bert and file=text.txt):



-i
s,bert,Say hurrah to &: /,
text.txt


If file.txt contains bert, the output will be:



Say hurrah to bert: /





share|improve this answer














The shell is responsible for expanding variables. When you use single quotes for strings, its contents will be treated literally, so sed now tries to replace every occurrence of the literal $var1 by ZZ.



Using double quotes



Use double quotes to make the shell expand variables while preserving whitespace:



sed -i "s/$var1/ZZ/g" "$file"


When you require the quote character in the replacement string, you have to precede it with a backslash which will be interpreted by the shell. In the following example, the string quote me will be replaced by "quote me" (the character & is interpreted by sed):



sed -i "s/quote me/"&"/" "$file"


Using single quotes



If you've a lot shell meta-characters, consider using single quotes for the pattern, and double quotes for the variable:



sed -i 's,'"$pattern"',Say hurrah to &: /,' "$file"


Notice how I use s,pattern,replacement, instead of s/pattern/replacement/, I did it to avoid interference with the / in /.



Example



The shell then runs the above command sed with the next arguments (assuming pattern=bert and file=text.txt):



-i
s,bert,Say hurrah to &: /,
text.txt


If file.txt contains bert, the output will be:



Say hurrah to bert: /






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 23 at 17:19

























answered Nov 7 '11 at 17:20









Lekensteyn

120k48263354




120k48263354








  • 1




    How would one pass the "/g" option using this comma separated form?
    – blong
    Apr 1 '14 at 13:55






  • 5




    @b.long It's a g option, so you would pass s,foo,bar,g instead.
    – Lekensteyn
    Apr 1 '14 at 14:32






  • 1




    your single quote example isparticularly useful, covers sed using regex pattern pretty nicely!
    – Brian Thomas
    Sep 9 '16 at 1:08












  • In case you are tempted to use \0 instead of , it should not be done when is enclosed in single quotes. Otherwise sed will substitute the pattern for a literal instead of the whole match.
    – Lekensteyn
    Aug 23 at 17:20














  • 1




    How would one pass the "/g" option using this comma separated form?
    – blong
    Apr 1 '14 at 13:55






  • 5




    @b.long It's a g option, so you would pass s,foo,bar,g instead.
    – Lekensteyn
    Apr 1 '14 at 14:32






  • 1




    your single quote example isparticularly useful, covers sed using regex pattern pretty nicely!
    – Brian Thomas
    Sep 9 '16 at 1:08












  • In case you are tempted to use \0 instead of , it should not be done when is enclosed in single quotes. Otherwise sed will substitute the pattern for a literal instead of the whole match.
    – Lekensteyn
    Aug 23 at 17:20








1




1




How would one pass the "/g" option using this comma separated form?
– blong
Apr 1 '14 at 13:55




How would one pass the "/g" option using this comma separated form?
– blong
Apr 1 '14 at 13:55




5




5




@b.long It's a g option, so you would pass s,foo,bar,g instead.
– Lekensteyn
Apr 1 '14 at 14:32




@b.long It's a g option, so you would pass s,foo,bar,g instead.
– Lekensteyn
Apr 1 '14 at 14:32




1




1




your single quote example isparticularly useful, covers sed using regex pattern pretty nicely!
– Brian Thomas
Sep 9 '16 at 1:08






your single quote example isparticularly useful, covers sed using regex pattern pretty nicely!
– Brian Thomas
Sep 9 '16 at 1:08














In case you are tempted to use \0 instead of , it should not be done when is enclosed in single quotes. Otherwise sed will substitute the pattern for a literal instead of the whole match.
– Lekensteyn
Aug 23 at 17:20




In case you are tempted to use \0 instead of , it should not be done when is enclosed in single quotes. Otherwise sed will substitute the pattern for a literal instead of the whole match.
– Lekensteyn
Aug 23 at 17:20












up vote
85
down vote













We can use variables in sed using double quotes:



sed -i "s/$var/r_str/g" file_name


If you have a slash / in the variable then use different separator, like below:



sed -i "s|$var|r_str|g" file_name





share|improve this answer



















  • 1




    If you have a slash / in the variable => This saved me ! My variable is a url and it contains /. Switching to use | as separator fixed my issue
    – sonlexqt
    Sep 20 at 4:39

















up vote
85
down vote













We can use variables in sed using double quotes:



sed -i "s/$var/r_str/g" file_name


If you have a slash / in the variable then use different separator, like below:



sed -i "s|$var|r_str|g" file_name





share|improve this answer



















  • 1




    If you have a slash / in the variable => This saved me ! My variable is a url and it contains /. Switching to use | as separator fixed my issue
    – sonlexqt
    Sep 20 at 4:39















up vote
85
down vote










up vote
85
down vote









We can use variables in sed using double quotes:



sed -i "s/$var/r_str/g" file_name


If you have a slash / in the variable then use different separator, like below:



sed -i "s|$var|r_str|g" file_name





share|improve this answer














We can use variables in sed using double quotes:



sed -i "s/$var/r_str/g" file_name


If you have a slash / in the variable then use different separator, like below:



sed -i "s|$var|r_str|g" file_name






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 3 '17 at 20:37









Zanna

49.3k13126236




49.3k13126236










answered Aug 7 '14 at 15:29









mani

85162




85162








  • 1




    If you have a slash / in the variable => This saved me ! My variable is a url and it contains /. Switching to use | as separator fixed my issue
    – sonlexqt
    Sep 20 at 4:39
















  • 1




    If you have a slash / in the variable => This saved me ! My variable is a url and it contains /. Switching to use | as separator fixed my issue
    – sonlexqt
    Sep 20 at 4:39










1




1




If you have a slash / in the variable => This saved me ! My variable is a url and it contains /. Switching to use | as separator fixed my issue
– sonlexqt
Sep 20 at 4:39






If you have a slash / in the variable => This saved me ! My variable is a url and it contains /. Switching to use | as separator fixed my issue
– sonlexqt
Sep 20 at 4:39







protected by Community Nov 30 at 16:21



Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



Would you like to answer one of these unanswered questions instead?



Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕