Shell: only double quote on test -n/-z? [duplicate]
Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
When is double-quoting necessary?
1 answer
In case of test -e
for example:
VAR="<this is a file path: /path/to/file/or/dir"
test -e $VAR && do_something || do_anotherthing
Question: Should I use "$VAR"
here?, here I don't like verbose if not necessary, because $VAR
obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary.
But it case of string test, test -n for exmpale:
VAR="<this is a string"
test -n $VAR && do_something || do_anotherthing
then with my logic, $VAR
should be put in double quote: "$VAR"
because it can be expanded to an empty string that if not in double quote will be expanded to -n
argument only and always true.
So the actual question because of that I'm in doubt of that should we only use double quotes in test command only with -n
and -z
against strings?
shell-script shell command-line shell-builtin
marked as duplicate by l0b0, mosvy, Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 2 at 22:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 3 more comments
This question already has an answer here:
When is double-quoting necessary?
1 answer
In case of test -e
for example:
VAR="<this is a file path: /path/to/file/or/dir"
test -e $VAR && do_something || do_anotherthing
Question: Should I use "$VAR"
here?, here I don't like verbose if not necessary, because $VAR
obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary.
But it case of string test, test -n for exmpale:
VAR="<this is a string"
test -n $VAR && do_something || do_anotherthing
then with my logic, $VAR
should be put in double quote: "$VAR"
because it can be expanded to an empty string that if not in double quote will be expanded to -n
argument only and always true.
So the actual question because of that I'm in doubt of that should we only use double quotes in test command only with -n
and -z
against strings?
shell-script shell command-line shell-builtin
marked as duplicate by l0b0, mosvy, Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 2 at 22:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I can't parse "Should I use"$VAR"
here?, here I don't like verbose if not necessary, because $VAR obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary." Can you please rephrase that, ideally with shorter sentences?
– l0b0
Jan 2 at 7:07
@l0b0: I just don't want to use quote if not necessary.
– Tuyen Pham
Jan 2 at 7:55
1
I'm sorry, but that is just the wrong approach in shell scripts.
– l0b0
Jan 2 at 8:31
@l0b0: But I see it as an abuse.
– Tuyen Pham
Jan 2 at 8:34
1
Filenames and paths can contain spaces. And tabs. And newlines. And wildcards. It's not safe to leave off double-quotes just because something's a path and "won't contain spaces".
– Gordon Davisson
Jan 2 at 9:56
|
show 3 more comments
This question already has an answer here:
When is double-quoting necessary?
1 answer
In case of test -e
for example:
VAR="<this is a file path: /path/to/file/or/dir"
test -e $VAR && do_something || do_anotherthing
Question: Should I use "$VAR"
here?, here I don't like verbose if not necessary, because $VAR
obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary.
But it case of string test, test -n for exmpale:
VAR="<this is a string"
test -n $VAR && do_something || do_anotherthing
then with my logic, $VAR
should be put in double quote: "$VAR"
because it can be expanded to an empty string that if not in double quote will be expanded to -n
argument only and always true.
So the actual question because of that I'm in doubt of that should we only use double quotes in test command only with -n
and -z
against strings?
shell-script shell command-line shell-builtin
This question already has an answer here:
When is double-quoting necessary?
1 answer
In case of test -e
for example:
VAR="<this is a file path: /path/to/file/or/dir"
test -e $VAR && do_something || do_anotherthing
Question: Should I use "$VAR"
here?, here I don't like verbose if not necessary, because $VAR
obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary.
But it case of string test, test -n for exmpale:
VAR="<this is a string"
test -n $VAR && do_something || do_anotherthing
then with my logic, $VAR
should be put in double quote: "$VAR"
because it can be expanded to an empty string that if not in double quote will be expanded to -n
argument only and always true.
So the actual question because of that I'm in doubt of that should we only use double quotes in test command only with -n
and -z
against strings?
This question already has an answer here:
When is double-quoting necessary?
1 answer
shell-script shell command-line shell-builtin
shell-script shell command-line shell-builtin
asked Jan 2 at 3:55
Tuyen PhamTuyen Pham
588114
588114
marked as duplicate by l0b0, mosvy, Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 2 at 22:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by l0b0, mosvy, Kusalananda
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 2 at 22:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I can't parse "Should I use"$VAR"
here?, here I don't like verbose if not necessary, because $VAR obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary." Can you please rephrase that, ideally with shorter sentences?
– l0b0
Jan 2 at 7:07
@l0b0: I just don't want to use quote if not necessary.
– Tuyen Pham
Jan 2 at 7:55
1
I'm sorry, but that is just the wrong approach in shell scripts.
– l0b0
Jan 2 at 8:31
@l0b0: But I see it as an abuse.
– Tuyen Pham
Jan 2 at 8:34
1
Filenames and paths can contain spaces. And tabs. And newlines. And wildcards. It's not safe to leave off double-quotes just because something's a path and "won't contain spaces".
– Gordon Davisson
Jan 2 at 9:56
|
show 3 more comments
I can't parse "Should I use"$VAR"
here?, here I don't like verbose if not necessary, because $VAR obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary." Can you please rephrase that, ideally with shorter sentences?
– l0b0
Jan 2 at 7:07
@l0b0: I just don't want to use quote if not necessary.
– Tuyen Pham
Jan 2 at 7:55
1
I'm sorry, but that is just the wrong approach in shell scripts.
– l0b0
Jan 2 at 8:31
@l0b0: But I see it as an abuse.
– Tuyen Pham
Jan 2 at 8:34
1
Filenames and paths can contain spaces. And tabs. And newlines. And wildcards. It's not safe to leave off double-quotes just because something's a path and "won't contain spaces".
– Gordon Davisson
Jan 2 at 9:56
I can't parse "Should I use
"$VAR"
here?, here I don't like verbose if not necessary, because $VAR obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary." Can you please rephrase that, ideally with shorter sentences?– l0b0
Jan 2 at 7:07
I can't parse "Should I use
"$VAR"
here?, here I don't like verbose if not necessary, because $VAR obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary." Can you please rephrase that, ideally with shorter sentences?– l0b0
Jan 2 at 7:07
@l0b0: I just don't want to use quote if not necessary.
– Tuyen Pham
Jan 2 at 7:55
@l0b0: I just don't want to use quote if not necessary.
– Tuyen Pham
Jan 2 at 7:55
1
1
I'm sorry, but that is just the wrong approach in shell scripts.
– l0b0
Jan 2 at 8:31
I'm sorry, but that is just the wrong approach in shell scripts.
– l0b0
Jan 2 at 8:31
@l0b0: But I see it as an abuse.
– Tuyen Pham
Jan 2 at 8:34
@l0b0: But I see it as an abuse.
– Tuyen Pham
Jan 2 at 8:34
1
1
Filenames and paths can contain spaces. And tabs. And newlines. And wildcards. It's not safe to leave off double-quotes just because something's a path and "won't contain spaces".
– Gordon Davisson
Jan 2 at 9:56
Filenames and paths can contain spaces. And tabs. And newlines. And wildcards. It's not safe to leave off double-quotes just because something's a path and "won't contain spaces".
– Gordon Davisson
Jan 2 at 9:56
|
show 3 more comments
3 Answers
3
active
oldest
votes
A general rule is to double quote any variable, unless you want the shell to perform token splitting and wildcard expansion on its content.
because
$VAR
obviously in this case is a path then if it's empty string it should always fail [...] then with my logic, double quote it is not necessary.
On contrary. The behavior of test -e
with no operands is another reason you should quote the variable in this particular case:
$ unset foo # or foo=""
$ test -e $foo # note this is equivalent to `test -e'
$ echo $?
0
$ test -e "$foo"
$ echo $?
1
$
Guess we should always double quotes variable in test command. Even though specific in my case,$VAR
is forbidden from being an empty string in the first place.
– Tuyen Pham
Jan 2 at 7:53
@TuyenPham, the typical problem is filenames that contain spaces. IfVAR="some file"
thentest -e $VAR
without quotes will result in an error "binary operator expected"
– glenn jackman
Jan 2 at 20:55
add a comment |
See this Q&A, Greg's Wiki and the manual for explanations. tl;dr: it is not at all obvious how this works, and you should Use More Quotes™. Or to put it a different way: if your goals are correctness and maintainability, quoting properly is approximately priority 5 (after learning all the other weirdness necessary to write correct and concise shell scripts), while saving individual characters is priority 999,999+.
There are places it's safe to leave the double-quotes off, but figuring out where it's safe (and tracking down & fixing the bugs when you get it wrong) is more trouble than just using double-quotes consistently in the first place.
– Gordon Davisson
Jan 2 at 9:52
add a comment |
This would be better, clearer:
if [[ "$VAR" ]]; then
do_something
else
do_anotherthing
fi
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
A general rule is to double quote any variable, unless you want the shell to perform token splitting and wildcard expansion on its content.
because
$VAR
obviously in this case is a path then if it's empty string it should always fail [...] then with my logic, double quote it is not necessary.
On contrary. The behavior of test -e
with no operands is another reason you should quote the variable in this particular case:
$ unset foo # or foo=""
$ test -e $foo # note this is equivalent to `test -e'
$ echo $?
0
$ test -e "$foo"
$ echo $?
1
$
Guess we should always double quotes variable in test command. Even though specific in my case,$VAR
is forbidden from being an empty string in the first place.
– Tuyen Pham
Jan 2 at 7:53
@TuyenPham, the typical problem is filenames that contain spaces. IfVAR="some file"
thentest -e $VAR
without quotes will result in an error "binary operator expected"
– glenn jackman
Jan 2 at 20:55
add a comment |
A general rule is to double quote any variable, unless you want the shell to perform token splitting and wildcard expansion on its content.
because
$VAR
obviously in this case is a path then if it's empty string it should always fail [...] then with my logic, double quote it is not necessary.
On contrary. The behavior of test -e
with no operands is another reason you should quote the variable in this particular case:
$ unset foo # or foo=""
$ test -e $foo # note this is equivalent to `test -e'
$ echo $?
0
$ test -e "$foo"
$ echo $?
1
$
Guess we should always double quotes variable in test command. Even though specific in my case,$VAR
is forbidden from being an empty string in the first place.
– Tuyen Pham
Jan 2 at 7:53
@TuyenPham, the typical problem is filenames that contain spaces. IfVAR="some file"
thentest -e $VAR
without quotes will result in an error "binary operator expected"
– glenn jackman
Jan 2 at 20:55
add a comment |
A general rule is to double quote any variable, unless you want the shell to perform token splitting and wildcard expansion on its content.
because
$VAR
obviously in this case is a path then if it's empty string it should always fail [...] then with my logic, double quote it is not necessary.
On contrary. The behavior of test -e
with no operands is another reason you should quote the variable in this particular case:
$ unset foo # or foo=""
$ test -e $foo # note this is equivalent to `test -e'
$ echo $?
0
$ test -e "$foo"
$ echo $?
1
$
A general rule is to double quote any variable, unless you want the shell to perform token splitting and wildcard expansion on its content.
because
$VAR
obviously in this case is a path then if it's empty string it should always fail [...] then with my logic, double quote it is not necessary.
On contrary. The behavior of test -e
with no operands is another reason you should quote the variable in this particular case:
$ unset foo # or foo=""
$ test -e $foo # note this is equivalent to `test -e'
$ echo $?
0
$ test -e "$foo"
$ echo $?
1
$
answered Jan 2 at 7:12
Kamil MaciorowskiKamil Maciorowski
1,2591625
1,2591625
Guess we should always double quotes variable in test command. Even though specific in my case,$VAR
is forbidden from being an empty string in the first place.
– Tuyen Pham
Jan 2 at 7:53
@TuyenPham, the typical problem is filenames that contain spaces. IfVAR="some file"
thentest -e $VAR
without quotes will result in an error "binary operator expected"
– glenn jackman
Jan 2 at 20:55
add a comment |
Guess we should always double quotes variable in test command. Even though specific in my case,$VAR
is forbidden from being an empty string in the first place.
– Tuyen Pham
Jan 2 at 7:53
@TuyenPham, the typical problem is filenames that contain spaces. IfVAR="some file"
thentest -e $VAR
without quotes will result in an error "binary operator expected"
– glenn jackman
Jan 2 at 20:55
Guess we should always double quotes variable in test command. Even though specific in my case,
$VAR
is forbidden from being an empty string in the first place.– Tuyen Pham
Jan 2 at 7:53
Guess we should always double quotes variable in test command. Even though specific in my case,
$VAR
is forbidden from being an empty string in the first place.– Tuyen Pham
Jan 2 at 7:53
@TuyenPham, the typical problem is filenames that contain spaces. If
VAR="some file"
then test -e $VAR
without quotes will result in an error "binary operator expected"– glenn jackman
Jan 2 at 20:55
@TuyenPham, the typical problem is filenames that contain spaces. If
VAR="some file"
then test -e $VAR
without quotes will result in an error "binary operator expected"– glenn jackman
Jan 2 at 20:55
add a comment |
See this Q&A, Greg's Wiki and the manual for explanations. tl;dr: it is not at all obvious how this works, and you should Use More Quotes™. Or to put it a different way: if your goals are correctness and maintainability, quoting properly is approximately priority 5 (after learning all the other weirdness necessary to write correct and concise shell scripts), while saving individual characters is priority 999,999+.
There are places it's safe to leave the double-quotes off, but figuring out where it's safe (and tracking down & fixing the bugs when you get it wrong) is more trouble than just using double-quotes consistently in the first place.
– Gordon Davisson
Jan 2 at 9:52
add a comment |
See this Q&A, Greg's Wiki and the manual for explanations. tl;dr: it is not at all obvious how this works, and you should Use More Quotes™. Or to put it a different way: if your goals are correctness and maintainability, quoting properly is approximately priority 5 (after learning all the other weirdness necessary to write correct and concise shell scripts), while saving individual characters is priority 999,999+.
There are places it's safe to leave the double-quotes off, but figuring out where it's safe (and tracking down & fixing the bugs when you get it wrong) is more trouble than just using double-quotes consistently in the first place.
– Gordon Davisson
Jan 2 at 9:52
add a comment |
See this Q&A, Greg's Wiki and the manual for explanations. tl;dr: it is not at all obvious how this works, and you should Use More Quotes™. Or to put it a different way: if your goals are correctness and maintainability, quoting properly is approximately priority 5 (after learning all the other weirdness necessary to write correct and concise shell scripts), while saving individual characters is priority 999,999+.
See this Q&A, Greg's Wiki and the manual for explanations. tl;dr: it is not at all obvious how this works, and you should Use More Quotes™. Or to put it a different way: if your goals are correctness and maintainability, quoting properly is approximately priority 5 (after learning all the other weirdness necessary to write correct and concise shell scripts), while saving individual characters is priority 999,999+.
edited Jan 2 at 22:13
answered Jan 2 at 7:09
l0b0l0b0
27.7k17117243
27.7k17117243
There are places it's safe to leave the double-quotes off, but figuring out where it's safe (and tracking down & fixing the bugs when you get it wrong) is more trouble than just using double-quotes consistently in the first place.
– Gordon Davisson
Jan 2 at 9:52
add a comment |
There are places it's safe to leave the double-quotes off, but figuring out where it's safe (and tracking down & fixing the bugs when you get it wrong) is more trouble than just using double-quotes consistently in the first place.
– Gordon Davisson
Jan 2 at 9:52
There are places it's safe to leave the double-quotes off, but figuring out where it's safe (and tracking down & fixing the bugs when you get it wrong) is more trouble than just using double-quotes consistently in the first place.
– Gordon Davisson
Jan 2 at 9:52
There are places it's safe to leave the double-quotes off, but figuring out where it's safe (and tracking down & fixing the bugs when you get it wrong) is more trouble than just using double-quotes consistently in the first place.
– Gordon Davisson
Jan 2 at 9:52
add a comment |
This would be better, clearer:
if [[ "$VAR" ]]; then
do_something
else
do_anotherthing
fi
add a comment |
This would be better, clearer:
if [[ "$VAR" ]]; then
do_something
else
do_anotherthing
fi
add a comment |
This would be better, clearer:
if [[ "$VAR" ]]; then
do_something
else
do_anotherthing
fi
This would be better, clearer:
if [[ "$VAR" ]]; then
do_something
else
do_anotherthing
fi
answered Jan 2 at 4:36
wefwef
1944
1944
add a comment |
add a comment |
I can't parse "Should I use
"$VAR"
here?, here I don't like verbose if not necessary, because $VAR obviously in this case is a path then if it's empty string it should always fail because there's no path that is empty string, then with my logic, double quote it is not necessary." Can you please rephrase that, ideally with shorter sentences?– l0b0
Jan 2 at 7:07
@l0b0: I just don't want to use quote if not necessary.
– Tuyen Pham
Jan 2 at 7:55
1
I'm sorry, but that is just the wrong approach in shell scripts.
– l0b0
Jan 2 at 8:31
@l0b0: But I see it as an abuse.
– Tuyen Pham
Jan 2 at 8:34
1
Filenames and paths can contain spaces. And tabs. And newlines. And wildcards. It's not safe to leave off double-quotes just because something's a path and "won't contain spaces".
– Gordon Davisson
Jan 2 at 9:56