Do I need to double quote a variable?
Clash 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
shell-script shell sed quoting variable
add a comment |Â
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
shell-script shell sed quoting variable
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 theMY_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
add a comment |Â
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
shell-script shell sed quoting variable
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
shell-script shell sed quoting variable
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 theMY_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
add a comment |Â
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 theMY_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
add a comment |Â
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).
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 thatsed
(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 isd
in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in thesed
manual.
â Kusalananda
Oct 20 '17 at 20:28
 |Â
show 5 more comments
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).
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 thatsed
(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 isd
in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in thesed
manual.
â Kusalananda
Oct 20 '17 at 20:28
 |Â
show 5 more comments
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).
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 thatsed
(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 isd
in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in thesed
manual.
â Kusalananda
Oct 20 '17 at 20:28
 |Â
show 5 more comments
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).
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).
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 thatsed
(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 isd
in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in thesed
manual.
â Kusalananda
Oct 20 '17 at 20:28
 |Â
show 5 more comments
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 thatsed
(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 isd
in this case), but if the character is not a slash, then it needs to be preceded by a backslash. This is described in thesed
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
 |Â
show 5 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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