Do I need to double quote a variable?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I am confused on when we double quote shell variables.

Specifically I am using the following sed replace command:



sed -i.tmp "/$MY_VAR/d" /foo/bar/file.txt 


But I am not quoting $MY_VAR. Is this correct? How can I quote it if not?

The following obviously does not work



sed -i.tmp '/"$MY_VAR"/d' /foo/bar/file.txt 






share|improve this question


















  • 2




    “Is it correct?” That depends on what you’re trying to do... Do you want to delete lines containing the “$MY_VAR” string (literally), or lines containing the value of the MY_VAR shell variable?
    – Stephen Kitt
    Oct 20 '17 at 14:33










  • show the $MY_VAR contents
    – RomanPerekhrest
    Oct 20 '17 at 14:33










  • @StephenKitt: The value of course
    – Jim
    Oct 20 '17 at 14:34






  • 1




    use double quotes when you want the shell to replace a variable with a value, and use single quotes when you want the literal variable name.
    – Tim Kennedy
    Oct 20 '17 at 14:46










  • @RomanPerekhrest: It is a string containing whitespaces no other special chars
    – Jim
    Oct 20 '17 at 16:30














up vote
0
down vote

favorite












I am confused on when we double quote shell variables.

Specifically I am using the following sed replace command:



sed -i.tmp "/$MY_VAR/d" /foo/bar/file.txt 


But I am not quoting $MY_VAR. Is this correct? How can I quote it if not?

The following obviously does not work



sed -i.tmp '/"$MY_VAR"/d' /foo/bar/file.txt 






share|improve this question


















  • 2




    “Is it correct?” That depends on what you’re trying to do... Do you want to delete lines containing the “$MY_VAR” string (literally), or lines containing the value of the MY_VAR shell variable?
    – Stephen Kitt
    Oct 20 '17 at 14:33










  • show the $MY_VAR contents
    – RomanPerekhrest
    Oct 20 '17 at 14:33










  • @StephenKitt: The value of course
    – Jim
    Oct 20 '17 at 14:34






  • 1




    use double quotes when you want the shell to replace a variable with a value, and use single quotes when you want the literal variable name.
    – Tim Kennedy
    Oct 20 '17 at 14:46










  • @RomanPerekhrest: It is a string containing whitespaces no other special chars
    – Jim
    Oct 20 '17 at 16:30












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am confused on when we double quote shell variables.

Specifically I am using the following sed replace command:



sed -i.tmp "/$MY_VAR/d" /foo/bar/file.txt 


But I am not quoting $MY_VAR. Is this correct? How can I quote it if not?

The following obviously does not work



sed -i.tmp '/"$MY_VAR"/d' /foo/bar/file.txt 






share|improve this question














I am confused on when we double quote shell variables.

Specifically I am using the following sed replace command:



sed -i.tmp "/$MY_VAR/d" /foo/bar/file.txt 


But I am not quoting $MY_VAR. Is this correct? How can I quote it if not?

The following obviously does not work



sed -i.tmp '/"$MY_VAR"/d' /foo/bar/file.txt 








share|improve this question













share|improve this question




share|improve this question








edited Oct 20 '17 at 18:54









kiamlaluno

362220




362220










asked Oct 20 '17 at 14:29









Jim

2,771113256




2,771113256







  • 2




    “Is it correct?” That depends on what you’re trying to do... Do you want to delete lines containing the “$MY_VAR” string (literally), or lines containing the value of the MY_VAR shell variable?
    – Stephen Kitt
    Oct 20 '17 at 14:33










  • show the $MY_VAR contents
    – RomanPerekhrest
    Oct 20 '17 at 14:33










  • @StephenKitt: The value of course
    – Jim
    Oct 20 '17 at 14:34






  • 1




    use double quotes when you want the shell to replace a variable with a value, and use single quotes when you want the literal variable name.
    – Tim Kennedy
    Oct 20 '17 at 14:46










  • @RomanPerekhrest: It is a string containing whitespaces no other special chars
    – Jim
    Oct 20 '17 at 16:30












  • 2




    “Is it correct?” That depends on what you’re trying to do... Do you want to delete lines containing the “$MY_VAR” string (literally), or lines containing the value of the MY_VAR shell variable?
    – Stephen Kitt
    Oct 20 '17 at 14:33










  • show the $MY_VAR contents
    – RomanPerekhrest
    Oct 20 '17 at 14:33










  • @StephenKitt: The value of course
    – Jim
    Oct 20 '17 at 14:34






  • 1




    use double quotes when you want the shell to replace a variable with a value, and use single quotes when you want the literal variable name.
    – Tim Kennedy
    Oct 20 '17 at 14:46










  • @RomanPerekhrest: It is a string containing whitespaces no other special chars
    – Jim
    Oct 20 '17 at 16:30







2




2




“Is it correct?” That depends on what you’re trying to do... Do you want to delete lines containing the “$MY_VAR” string (literally), or lines containing the value of the MY_VAR shell variable?
– Stephen Kitt
Oct 20 '17 at 14:33




“Is it correct?” That depends on what you’re trying to do... Do you want to delete lines containing the “$MY_VAR” string (literally), or lines containing the value of the MY_VAR shell variable?
– Stephen Kitt
Oct 20 '17 at 14:33












show the $MY_VAR contents
– RomanPerekhrest
Oct 20 '17 at 14:33




show the $MY_VAR contents
– RomanPerekhrest
Oct 20 '17 at 14:33












@StephenKitt: The value of course
– Jim
Oct 20 '17 at 14:34




@StephenKitt: The value of course
– Jim
Oct 20 '17 at 14:34




1




1




use double quotes when you want the shell to replace a variable with a value, and use single quotes when you want the literal variable name.
– Tim Kennedy
Oct 20 '17 at 14:46




use double quotes when you want the shell to replace a variable with a value, and use single quotes when you want the literal variable name.
– Tim Kennedy
Oct 20 '17 at 14:46












@RomanPerekhrest: It is a string containing whitespaces no other special chars
– Jim
Oct 20 '17 at 16:30




@RomanPerekhrest: It is a string containing whitespaces no other special chars
– Jim
Oct 20 '17 at 16:30










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










In your first command, you are quoting the variable.



The string passed as the second command line argument to sed has double quotes around it, and the variable is within those quotes and will be expanded by the shell.



The second command will not work (as expected), as you point out, because the shell would not expand the value of the variable since it's quoted using single quotes. It will still do something though. It will delete any line of input containing the literal string "$MY_VAR" (including the quotes).



The first command is correct, but you will have issues if $MY_VAR contains slashes. If it does, pick a delimiter for the sed pattern that does not occur in $MY_VAR:



sed "@$MY_VAR@d"



A variable is quoted when it appears in quotes. The variable does not need to be "tightly quoted" to be quoted. That is, within the string "hello $world!", the variable $world is quoted even though it does not appear as "$world".



What matters is that the string as a whole is quoted. If double quotes are used, then the shell will expand any variables within it.



In the example above, the string "@$MY_VAR@d" is quoted, and the variable $MY_VAR is within it, so it is quoted as well (since it's within the quoted string).






share|improve this answer






















  • My understanding is that the best practice is to always quote shell variables i.e. always use "$MY_VAR" than $MY_VAR. So I was trying to understand if it is applicable to this case. You also in your solution snippet do not quote the variable itself.
    – Jim
    Oct 20 '17 at 16:34










  • @Jim The variable will be expanded within double quotes. It is quoted. The double quotes do not have to be "tightly around the variable". What counts is that sed (in this example) gets a single string.
    – Kusalananda
    Oct 20 '17 at 17:01










  • They don't have to be tightly around the variable? Then I have completely misunderstood the recommendation. An example post unix.stackexchange.com/questions/68694/…
    – Jim
    Oct 20 '17 at 20:23










  • And why did @ need to be escaped in "@$MY_VAR@d"?
    – Jim
    Oct 20 '17 at 20:24






  • 1




    @Jim Any character (other than backslash and newline) can be the delimiter for the regular expression that acts like the address for the command (the command is d in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in the sed manual.
    – Kusalananda
    Oct 20 '17 at 20:28










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399358%2fdo-i-need-to-double-quote-a-variable%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










In your first command, you are quoting the variable.



The string passed as the second command line argument to sed has double quotes around it, and the variable is within those quotes and will be expanded by the shell.



The second command will not work (as expected), as you point out, because the shell would not expand the value of the variable since it's quoted using single quotes. It will still do something though. It will delete any line of input containing the literal string "$MY_VAR" (including the quotes).



The first command is correct, but you will have issues if $MY_VAR contains slashes. If it does, pick a delimiter for the sed pattern that does not occur in $MY_VAR:



sed "@$MY_VAR@d"



A variable is quoted when it appears in quotes. The variable does not need to be "tightly quoted" to be quoted. That is, within the string "hello $world!", the variable $world is quoted even though it does not appear as "$world".



What matters is that the string as a whole is quoted. If double quotes are used, then the shell will expand any variables within it.



In the example above, the string "@$MY_VAR@d" is quoted, and the variable $MY_VAR is within it, so it is quoted as well (since it's within the quoted string).






share|improve this answer






















  • My understanding is that the best practice is to always quote shell variables i.e. always use "$MY_VAR" than $MY_VAR. So I was trying to understand if it is applicable to this case. You also in your solution snippet do not quote the variable itself.
    – Jim
    Oct 20 '17 at 16:34










  • @Jim The variable will be expanded within double quotes. It is quoted. The double quotes do not have to be "tightly around the variable". What counts is that sed (in this example) gets a single string.
    – Kusalananda
    Oct 20 '17 at 17:01










  • They don't have to be tightly around the variable? Then I have completely misunderstood the recommendation. An example post unix.stackexchange.com/questions/68694/…
    – Jim
    Oct 20 '17 at 20:23










  • And why did @ need to be escaped in "@$MY_VAR@d"?
    – Jim
    Oct 20 '17 at 20:24






  • 1




    @Jim Any character (other than backslash and newline) can be the delimiter for the regular expression that acts like the address for the command (the command is d in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in the sed manual.
    – Kusalananda
    Oct 20 '17 at 20:28














up vote
3
down vote



accepted










In your first command, you are quoting the variable.



The string passed as the second command line argument to sed has double quotes around it, and the variable is within those quotes and will be expanded by the shell.



The second command will not work (as expected), as you point out, because the shell would not expand the value of the variable since it's quoted using single quotes. It will still do something though. It will delete any line of input containing the literal string "$MY_VAR" (including the quotes).



The first command is correct, but you will have issues if $MY_VAR contains slashes. If it does, pick a delimiter for the sed pattern that does not occur in $MY_VAR:



sed "@$MY_VAR@d"



A variable is quoted when it appears in quotes. The variable does not need to be "tightly quoted" to be quoted. That is, within the string "hello $world!", the variable $world is quoted even though it does not appear as "$world".



What matters is that the string as a whole is quoted. If double quotes are used, then the shell will expand any variables within it.



In the example above, the string "@$MY_VAR@d" is quoted, and the variable $MY_VAR is within it, so it is quoted as well (since it's within the quoted string).






share|improve this answer






















  • My understanding is that the best practice is to always quote shell variables i.e. always use "$MY_VAR" than $MY_VAR. So I was trying to understand if it is applicable to this case. You also in your solution snippet do not quote the variable itself.
    – Jim
    Oct 20 '17 at 16:34










  • @Jim The variable will be expanded within double quotes. It is quoted. The double quotes do not have to be "tightly around the variable". What counts is that sed (in this example) gets a single string.
    – Kusalananda
    Oct 20 '17 at 17:01










  • They don't have to be tightly around the variable? Then I have completely misunderstood the recommendation. An example post unix.stackexchange.com/questions/68694/…
    – Jim
    Oct 20 '17 at 20:23










  • And why did @ need to be escaped in "@$MY_VAR@d"?
    – Jim
    Oct 20 '17 at 20:24






  • 1




    @Jim Any character (other than backslash and newline) can be the delimiter for the regular expression that acts like the address for the command (the command is d in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in the sed manual.
    – Kusalananda
    Oct 20 '17 at 20:28












up vote
3
down vote



accepted







up vote
3
down vote



accepted






In your first command, you are quoting the variable.



The string passed as the second command line argument to sed has double quotes around it, and the variable is within those quotes and will be expanded by the shell.



The second command will not work (as expected), as you point out, because the shell would not expand the value of the variable since it's quoted using single quotes. It will still do something though. It will delete any line of input containing the literal string "$MY_VAR" (including the quotes).



The first command is correct, but you will have issues if $MY_VAR contains slashes. If it does, pick a delimiter for the sed pattern that does not occur in $MY_VAR:



sed "@$MY_VAR@d"



A variable is quoted when it appears in quotes. The variable does not need to be "tightly quoted" to be quoted. That is, within the string "hello $world!", the variable $world is quoted even though it does not appear as "$world".



What matters is that the string as a whole is quoted. If double quotes are used, then the shell will expand any variables within it.



In the example above, the string "@$MY_VAR@d" is quoted, and the variable $MY_VAR is within it, so it is quoted as well (since it's within the quoted string).






share|improve this answer














In your first command, you are quoting the variable.



The string passed as the second command line argument to sed has double quotes around it, and the variable is within those quotes and will be expanded by the shell.



The second command will not work (as expected), as you point out, because the shell would not expand the value of the variable since it's quoted using single quotes. It will still do something though. It will delete any line of input containing the literal string "$MY_VAR" (including the quotes).



The first command is correct, but you will have issues if $MY_VAR contains slashes. If it does, pick a delimiter for the sed pattern that does not occur in $MY_VAR:



sed "@$MY_VAR@d"



A variable is quoted when it appears in quotes. The variable does not need to be "tightly quoted" to be quoted. That is, within the string "hello $world!", the variable $world is quoted even though it does not appear as "$world".



What matters is that the string as a whole is quoted. If double quotes are used, then the shell will expand any variables within it.



In the example above, the string "@$MY_VAR@d" is quoted, and the variable $MY_VAR is within it, so it is quoted as well (since it's within the quoted string).







share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 20 '17 at 20:47

























answered Oct 20 '17 at 14:34









Kusalananda

105k14209326




105k14209326











  • My understanding is that the best practice is to always quote shell variables i.e. always use "$MY_VAR" than $MY_VAR. So I was trying to understand if it is applicable to this case. You also in your solution snippet do not quote the variable itself.
    – Jim
    Oct 20 '17 at 16:34










  • @Jim The variable will be expanded within double quotes. It is quoted. The double quotes do not have to be "tightly around the variable". What counts is that sed (in this example) gets a single string.
    – Kusalananda
    Oct 20 '17 at 17:01










  • They don't have to be tightly around the variable? Then I have completely misunderstood the recommendation. An example post unix.stackexchange.com/questions/68694/…
    – Jim
    Oct 20 '17 at 20:23










  • And why did @ need to be escaped in "@$MY_VAR@d"?
    – Jim
    Oct 20 '17 at 20:24






  • 1




    @Jim Any character (other than backslash and newline) can be the delimiter for the regular expression that acts like the address for the command (the command is d in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in the sed manual.
    – Kusalananda
    Oct 20 '17 at 20:28
















  • My understanding is that the best practice is to always quote shell variables i.e. always use "$MY_VAR" than $MY_VAR. So I was trying to understand if it is applicable to this case. You also in your solution snippet do not quote the variable itself.
    – Jim
    Oct 20 '17 at 16:34










  • @Jim The variable will be expanded within double quotes. It is quoted. The double quotes do not have to be "tightly around the variable". What counts is that sed (in this example) gets a single string.
    – Kusalananda
    Oct 20 '17 at 17:01










  • They don't have to be tightly around the variable? Then I have completely misunderstood the recommendation. An example post unix.stackexchange.com/questions/68694/…
    – Jim
    Oct 20 '17 at 20:23










  • And why did @ need to be escaped in "@$MY_VAR@d"?
    – Jim
    Oct 20 '17 at 20:24






  • 1




    @Jim Any character (other than backslash and newline) can be the delimiter for the regular expression that acts like the address for the command (the command is d in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in the sed manual.
    – Kusalananda
    Oct 20 '17 at 20:28















My understanding is that the best practice is to always quote shell variables i.e. always use "$MY_VAR" than $MY_VAR. So I was trying to understand if it is applicable to this case. You also in your solution snippet do not quote the variable itself.
– Jim
Oct 20 '17 at 16:34




My understanding is that the best practice is to always quote shell variables i.e. always use "$MY_VAR" than $MY_VAR. So I was trying to understand if it is applicable to this case. You also in your solution snippet do not quote the variable itself.
– Jim
Oct 20 '17 at 16:34












@Jim The variable will be expanded within double quotes. It is quoted. The double quotes do not have to be "tightly around the variable". What counts is that sed (in this example) gets a single string.
– Kusalananda
Oct 20 '17 at 17:01




@Jim The variable will be expanded within double quotes. It is quoted. The double quotes do not have to be "tightly around the variable". What counts is that sed (in this example) gets a single string.
– Kusalananda
Oct 20 '17 at 17:01












They don't have to be tightly around the variable? Then I have completely misunderstood the recommendation. An example post unix.stackexchange.com/questions/68694/…
– Jim
Oct 20 '17 at 20:23




They don't have to be tightly around the variable? Then I have completely misunderstood the recommendation. An example post unix.stackexchange.com/questions/68694/…
– Jim
Oct 20 '17 at 20:23












And why did @ need to be escaped in "@$MY_VAR@d"?
– Jim
Oct 20 '17 at 20:24




And why did @ need to be escaped in "@$MY_VAR@d"?
– Jim
Oct 20 '17 at 20:24




1




1




@Jim Any character (other than backslash and newline) can be the delimiter for the regular expression that acts like the address for the command (the command is d in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in the sed manual.
– Kusalananda
Oct 20 '17 at 20:28




@Jim Any character (other than backslash and newline) can be the delimiter for the regular expression that acts like the address for the command (the command is d in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in the sed manual.
– Kusalananda
Oct 20 '17 at 20:28

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399358%2fdo-i-need-to-double-quote-a-variable%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay