Shell: only double quote on test -n/-z? [duplicate]

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












1
















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?










share|improve this question













marked as duplicate by l0b0, mosvy, Kusalananda shell
Users with the  shell badge can single-handedly close shell questions as duplicates and reopen them as needed.

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















1
















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?










share|improve this question













marked as duplicate by l0b0, mosvy, Kusalananda shell
Users with the  shell badge can single-handedly close shell questions as duplicates and reopen them as needed.

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













1












1








1









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?










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 3:55









Tuyen PhamTuyen Pham

588114




588114




marked as duplicate by l0b0, mosvy, Kusalananda shell
Users with the  shell badge can single-handedly close shell questions as duplicates and reopen them as needed.

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 shell
Users with the  shell badge can single-handedly close shell questions as duplicates and reopen them as needed.

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

















  • 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










3 Answers
3






active

oldest

votes


















4














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
$





share|improve this answer























  • 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



















3














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+.






share|improve this answer

























  • 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


















-1














This would be better, clearer:



if [[ "$VAR" ]]; then
do_something
else
do_anotherthing
fi





share|improve this answer





























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    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
    $





    share|improve this answer























    • 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
















    4














    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
    $





    share|improve this answer























    • 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














    4












    4








    4







    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
    $





    share|improve this answer













    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
    $






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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. 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


















    • 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

















    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














    3














    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+.






    share|improve this answer

























    • 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















    3














    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+.






    share|improve this answer

























    • 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













    3












    3








    3







    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+.






    share|improve this answer















    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+.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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

















    • 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











    -1














    This would be better, clearer:



    if [[ "$VAR" ]]; then
    do_something
    else
    do_anotherthing
    fi





    share|improve this answer



























      -1














      This would be better, clearer:



      if [[ "$VAR" ]]; then
      do_something
      else
      do_anotherthing
      fi





      share|improve this answer

























        -1












        -1








        -1







        This would be better, clearer:



        if [[ "$VAR" ]]; then
        do_something
        else
        do_anotherthing
        fi





        share|improve this answer













        This would be better, clearer:



        if [[ "$VAR" ]]; then
        do_something
        else
        do_anotherthing
        fi






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 4:36









        wefwef

        1944




        1944












            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