Why does bash convert .* to hidden file list in current dir and how to prevent it from doing it?

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











up vote
-1
down vote

favorite












# list=(`echo ".*"`)
# for item in $list[@]; do echo "$item"; done;
.
..
.DS_Store
.git
.gitignore
.tox
.toxrc


In the code above, I tried to write a string with .* into an array in bash, but after doing that, the ".*" will be converted to a full file/dir list in current dir.
How can I stop bash from doing that?







share|improve this question















  • 2




    What result do you want to see instead?
    – JigglyNaga
    Jun 13 at 11:58










  • What problem do you see here? The only thing that is subject for a discussion is whether this should include "." and "..".
    – schily
    Jun 13 at 12:04










  • The double quotes will still allow the * to be expanded. What is the expected output that you want returned?
    – Nasir Riley
    Jun 13 at 12:08










  • The answer from @user4556274 resolved my problem.
    – LCB
    Jun 13 at 12:21














up vote
-1
down vote

favorite












# list=(`echo ".*"`)
# for item in $list[@]; do echo "$item"; done;
.
..
.DS_Store
.git
.gitignore
.tox
.toxrc


In the code above, I tried to write a string with .* into an array in bash, but after doing that, the ".*" will be converted to a full file/dir list in current dir.
How can I stop bash from doing that?







share|improve this question















  • 2




    What result do you want to see instead?
    – JigglyNaga
    Jun 13 at 11:58










  • What problem do you see here? The only thing that is subject for a discussion is whether this should include "." and "..".
    – schily
    Jun 13 at 12:04










  • The double quotes will still allow the * to be expanded. What is the expected output that you want returned?
    – Nasir Riley
    Jun 13 at 12:08










  • The answer from @user4556274 resolved my problem.
    – LCB
    Jun 13 at 12:21












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











# list=(`echo ".*"`)
# for item in $list[@]; do echo "$item"; done;
.
..
.DS_Store
.git
.gitignore
.tox
.toxrc


In the code above, I tried to write a string with .* into an array in bash, but after doing that, the ".*" will be converted to a full file/dir list in current dir.
How can I stop bash from doing that?







share|improve this question











# list=(`echo ".*"`)
# for item in $list[@]; do echo "$item"; done;
.
..
.DS_Store
.git
.gitignore
.tox
.toxrc


In the code above, I tried to write a string with .* into an array in bash, but after doing that, the ".*" will be converted to a full file/dir list in current dir.
How can I stop bash from doing that?









share|improve this question










share|improve this question




share|improve this question









asked Jun 13 at 11:51









LCB

1012




1012







  • 2




    What result do you want to see instead?
    – JigglyNaga
    Jun 13 at 11:58










  • What problem do you see here? The only thing that is subject for a discussion is whether this should include "." and "..".
    – schily
    Jun 13 at 12:04










  • The double quotes will still allow the * to be expanded. What is the expected output that you want returned?
    – Nasir Riley
    Jun 13 at 12:08










  • The answer from @user4556274 resolved my problem.
    – LCB
    Jun 13 at 12:21












  • 2




    What result do you want to see instead?
    – JigglyNaga
    Jun 13 at 11:58










  • What problem do you see here? The only thing that is subject for a discussion is whether this should include "." and "..".
    – schily
    Jun 13 at 12:04










  • The double quotes will still allow the * to be expanded. What is the expected output that you want returned?
    – Nasir Riley
    Jun 13 at 12:08










  • The answer from @user4556274 resolved my problem.
    – LCB
    Jun 13 at 12:21







2




2




What result do you want to see instead?
– JigglyNaga
Jun 13 at 11:58




What result do you want to see instead?
– JigglyNaga
Jun 13 at 11:58












What problem do you see here? The only thing that is subject for a discussion is whether this should include "." and "..".
– schily
Jun 13 at 12:04




What problem do you see here? The only thing that is subject for a discussion is whether this should include "." and "..".
– schily
Jun 13 at 12:04












The double quotes will still allow the * to be expanded. What is the expected output that you want returned?
– Nasir Riley
Jun 13 at 12:08




The double quotes will still allow the * to be expanded. What is the expected output that you want returned?
– Nasir Riley
Jun 13 at 12:08












The answer from @user4556274 resolved my problem.
– LCB
Jun 13 at 12:21




The answer from @user4556274 resolved my problem.
– LCB
Jun 13 at 12:21










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










From man bash, under options for set :



-f Disable pathname expansion.


So issue



set -f


to disable globbing, and then



set +f


to re-enable normal globbing behavior.




Slightly longer relevant excerpt from the man page:




Pathname Expansion



After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.







share|improve this answer

















  • 3




    This is not a good solution. The right way to address the issue is to use proper declaration so that the shell doesn't want to expand it. E.G. list=(".*"), as in TooTea's answer. The echo and backticks are useless.
    – Patrick
    Jun 13 at 12:24











  • @Patrick, I agree this is not a solution, as I am not clear on what the OP really wants to accomplish (XY). However it does answer the question in the title. Also, list=(".*") will also be expanded by bash if globbing is not disabled (apart from the echo subshell being useless).
    – user4556274
    Jun 13 at 13:03






  • 2




    "Also, list=(".*") will also be expanded by bash" -- This is incorrect. It will not.
    – Patrick
    Jun 13 at 13:37

















up vote
4
down vote













list=(`echo ".*"`)


In the above, you have no quotes around the command substitution, so the resulting output is subject to word splitting and globbing.



for item in $list[@]; do echo "$item"; done;


Also, here, you have no quotes around $list[@], so again, it's subject to word splitting and globbing. (This will matter if you add quotes to the assignment, or if any matching filenames contain whitespace or glob characters...)



If you just want the literal string .*, use list=(".*") and for item in "$list[@]"; .... If you want to use the command substitution, put quotes around it, i.e. "$(somecmd)".



See also:



  • What's the right way to quote $(command $arg)?

  • Why does my shell script choke on whitespace or other special characters?

  • When is double-quoting necessary?

  • WordSplitting on wiki.wooledge.org





share|improve this answer




























    up vote
    1
    down vote













    The echo … part in the backticks yields the (unquoted) string .*, which is subsequently expanded by BASH. If you don't want the ".*" to be expanded to a list of files, just use list=(".*"). However, I fail to see what the subsequent iteration over such an array would be good for.






    share|improve this answer























    • This isn't quite accurate. It's not the echo ... which is performing the expansion. It's when going from the backticks to the array. The backticks output is unquoted (which you can't quote without causing other issues, though $() would work), thus it gets expanded before populating the array. However your solution is accurate (remove the backticks and echo entirely).
      – Patrick
      Jun 13 at 12:21











    • You're right, the result of the backticks gets expanded. I got confused by the entirely useless echo there.
      – TooTea
      Jun 13 at 13:26










    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%2f449523%2fwhy-does-bash-convert-to-hidden-file-list-in-current-dir-and-how-to-prevent-i%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    From man bash, under options for set :



    -f Disable pathname expansion.


    So issue



    set -f


    to disable globbing, and then



    set +f


    to re-enable normal globbing behavior.




    Slightly longer relevant excerpt from the man page:




    Pathname Expansion



    After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.







    share|improve this answer

















    • 3




      This is not a good solution. The right way to address the issue is to use proper declaration so that the shell doesn't want to expand it. E.G. list=(".*"), as in TooTea's answer. The echo and backticks are useless.
      – Patrick
      Jun 13 at 12:24











    • @Patrick, I agree this is not a solution, as I am not clear on what the OP really wants to accomplish (XY). However it does answer the question in the title. Also, list=(".*") will also be expanded by bash if globbing is not disabled (apart from the echo subshell being useless).
      – user4556274
      Jun 13 at 13:03






    • 2




      "Also, list=(".*") will also be expanded by bash" -- This is incorrect. It will not.
      – Patrick
      Jun 13 at 13:37














    up vote
    2
    down vote



    accepted










    From man bash, under options for set :



    -f Disable pathname expansion.


    So issue



    set -f


    to disable globbing, and then



    set +f


    to re-enable normal globbing behavior.




    Slightly longer relevant excerpt from the man page:




    Pathname Expansion



    After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.







    share|improve this answer

















    • 3




      This is not a good solution. The right way to address the issue is to use proper declaration so that the shell doesn't want to expand it. E.G. list=(".*"), as in TooTea's answer. The echo and backticks are useless.
      – Patrick
      Jun 13 at 12:24











    • @Patrick, I agree this is not a solution, as I am not clear on what the OP really wants to accomplish (XY). However it does answer the question in the title. Also, list=(".*") will also be expanded by bash if globbing is not disabled (apart from the echo subshell being useless).
      – user4556274
      Jun 13 at 13:03






    • 2




      "Also, list=(".*") will also be expanded by bash" -- This is incorrect. It will not.
      – Patrick
      Jun 13 at 13:37












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    From man bash, under options for set :



    -f Disable pathname expansion.


    So issue



    set -f


    to disable globbing, and then



    set +f


    to re-enable normal globbing behavior.




    Slightly longer relevant excerpt from the man page:




    Pathname Expansion



    After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.







    share|improve this answer













    From man bash, under options for set :



    -f Disable pathname expansion.


    So issue



    set -f


    to disable globbing, and then



    set +f


    to re-enable normal globbing behavior.




    Slightly longer relevant excerpt from the man page:




    Pathname Expansion



    After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.








    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jun 13 at 12:02









    user4556274

    4,94811123




    4,94811123







    • 3




      This is not a good solution. The right way to address the issue is to use proper declaration so that the shell doesn't want to expand it. E.G. list=(".*"), as in TooTea's answer. The echo and backticks are useless.
      – Patrick
      Jun 13 at 12:24











    • @Patrick, I agree this is not a solution, as I am not clear on what the OP really wants to accomplish (XY). However it does answer the question in the title. Also, list=(".*") will also be expanded by bash if globbing is not disabled (apart from the echo subshell being useless).
      – user4556274
      Jun 13 at 13:03






    • 2




      "Also, list=(".*") will also be expanded by bash" -- This is incorrect. It will not.
      – Patrick
      Jun 13 at 13:37












    • 3




      This is not a good solution. The right way to address the issue is to use proper declaration so that the shell doesn't want to expand it. E.G. list=(".*"), as in TooTea's answer. The echo and backticks are useless.
      – Patrick
      Jun 13 at 12:24











    • @Patrick, I agree this is not a solution, as I am not clear on what the OP really wants to accomplish (XY). However it does answer the question in the title. Also, list=(".*") will also be expanded by bash if globbing is not disabled (apart from the echo subshell being useless).
      – user4556274
      Jun 13 at 13:03






    • 2




      "Also, list=(".*") will also be expanded by bash" -- This is incorrect. It will not.
      – Patrick
      Jun 13 at 13:37







    3




    3




    This is not a good solution. The right way to address the issue is to use proper declaration so that the shell doesn't want to expand it. E.G. list=(".*"), as in TooTea's answer. The echo and backticks are useless.
    – Patrick
    Jun 13 at 12:24





    This is not a good solution. The right way to address the issue is to use proper declaration so that the shell doesn't want to expand it. E.G. list=(".*"), as in TooTea's answer. The echo and backticks are useless.
    – Patrick
    Jun 13 at 12:24













    @Patrick, I agree this is not a solution, as I am not clear on what the OP really wants to accomplish (XY). However it does answer the question in the title. Also, list=(".*") will also be expanded by bash if globbing is not disabled (apart from the echo subshell being useless).
    – user4556274
    Jun 13 at 13:03




    @Patrick, I agree this is not a solution, as I am not clear on what the OP really wants to accomplish (XY). However it does answer the question in the title. Also, list=(".*") will also be expanded by bash if globbing is not disabled (apart from the echo subshell being useless).
    – user4556274
    Jun 13 at 13:03




    2




    2




    "Also, list=(".*") will also be expanded by bash" -- This is incorrect. It will not.
    – Patrick
    Jun 13 at 13:37




    "Also, list=(".*") will also be expanded by bash" -- This is incorrect. It will not.
    – Patrick
    Jun 13 at 13:37












    up vote
    4
    down vote













    list=(`echo ".*"`)


    In the above, you have no quotes around the command substitution, so the resulting output is subject to word splitting and globbing.



    for item in $list[@]; do echo "$item"; done;


    Also, here, you have no quotes around $list[@], so again, it's subject to word splitting and globbing. (This will matter if you add quotes to the assignment, or if any matching filenames contain whitespace or glob characters...)



    If you just want the literal string .*, use list=(".*") and for item in "$list[@]"; .... If you want to use the command substitution, put quotes around it, i.e. "$(somecmd)".



    See also:



    • What's the right way to quote $(command $arg)?

    • Why does my shell script choke on whitespace or other special characters?

    • When is double-quoting necessary?

    • WordSplitting on wiki.wooledge.org





    share|improve this answer

























      up vote
      4
      down vote













      list=(`echo ".*"`)


      In the above, you have no quotes around the command substitution, so the resulting output is subject to word splitting and globbing.



      for item in $list[@]; do echo "$item"; done;


      Also, here, you have no quotes around $list[@], so again, it's subject to word splitting and globbing. (This will matter if you add quotes to the assignment, or if any matching filenames contain whitespace or glob characters...)



      If you just want the literal string .*, use list=(".*") and for item in "$list[@]"; .... If you want to use the command substitution, put quotes around it, i.e. "$(somecmd)".



      See also:



      • What's the right way to quote $(command $arg)?

      • Why does my shell script choke on whitespace or other special characters?

      • When is double-quoting necessary?

      • WordSplitting on wiki.wooledge.org





      share|improve this answer























        up vote
        4
        down vote










        up vote
        4
        down vote









        list=(`echo ".*"`)


        In the above, you have no quotes around the command substitution, so the resulting output is subject to word splitting and globbing.



        for item in $list[@]; do echo "$item"; done;


        Also, here, you have no quotes around $list[@], so again, it's subject to word splitting and globbing. (This will matter if you add quotes to the assignment, or if any matching filenames contain whitespace or glob characters...)



        If you just want the literal string .*, use list=(".*") and for item in "$list[@]"; .... If you want to use the command substitution, put quotes around it, i.e. "$(somecmd)".



        See also:



        • What's the right way to quote $(command $arg)?

        • Why does my shell script choke on whitespace or other special characters?

        • When is double-quoting necessary?

        • WordSplitting on wiki.wooledge.org





        share|improve this answer













        list=(`echo ".*"`)


        In the above, you have no quotes around the command substitution, so the resulting output is subject to word splitting and globbing.



        for item in $list[@]; do echo "$item"; done;


        Also, here, you have no quotes around $list[@], so again, it's subject to word splitting and globbing. (This will matter if you add quotes to the assignment, or if any matching filenames contain whitespace or glob characters...)



        If you just want the literal string .*, use list=(".*") and for item in "$list[@]"; .... If you want to use the command substitution, put quotes around it, i.e. "$(somecmd)".



        See also:



        • What's the right way to quote $(command $arg)?

        • Why does my shell script choke on whitespace or other special characters?

        • When is double-quoting necessary?

        • WordSplitting on wiki.wooledge.org






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 13 at 13:29









        ilkkachu

        47.5k668130




        47.5k668130




















            up vote
            1
            down vote













            The echo … part in the backticks yields the (unquoted) string .*, which is subsequently expanded by BASH. If you don't want the ".*" to be expanded to a list of files, just use list=(".*"). However, I fail to see what the subsequent iteration over such an array would be good for.






            share|improve this answer























            • This isn't quite accurate. It's not the echo ... which is performing the expansion. It's when going from the backticks to the array. The backticks output is unquoted (which you can't quote without causing other issues, though $() would work), thus it gets expanded before populating the array. However your solution is accurate (remove the backticks and echo entirely).
              – Patrick
              Jun 13 at 12:21











            • You're right, the result of the backticks gets expanded. I got confused by the entirely useless echo there.
              – TooTea
              Jun 13 at 13:26














            up vote
            1
            down vote













            The echo … part in the backticks yields the (unquoted) string .*, which is subsequently expanded by BASH. If you don't want the ".*" to be expanded to a list of files, just use list=(".*"). However, I fail to see what the subsequent iteration over such an array would be good for.






            share|improve this answer























            • This isn't quite accurate. It's not the echo ... which is performing the expansion. It's when going from the backticks to the array. The backticks output is unquoted (which you can't quote without causing other issues, though $() would work), thus it gets expanded before populating the array. However your solution is accurate (remove the backticks and echo entirely).
              – Patrick
              Jun 13 at 12:21











            • You're right, the result of the backticks gets expanded. I got confused by the entirely useless echo there.
              – TooTea
              Jun 13 at 13:26












            up vote
            1
            down vote










            up vote
            1
            down vote









            The echo … part in the backticks yields the (unquoted) string .*, which is subsequently expanded by BASH. If you don't want the ".*" to be expanded to a list of files, just use list=(".*"). However, I fail to see what the subsequent iteration over such an array would be good for.






            share|improve this answer















            The echo … part in the backticks yields the (unquoted) string .*, which is subsequently expanded by BASH. If you don't want the ".*" to be expanded to a list of files, just use list=(".*"). However, I fail to see what the subsequent iteration over such an array would be good for.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jun 13 at 13:29


























            answered Jun 13 at 12:02









            TooTea

            2715




            2715











            • This isn't quite accurate. It's not the echo ... which is performing the expansion. It's when going from the backticks to the array. The backticks output is unquoted (which you can't quote without causing other issues, though $() would work), thus it gets expanded before populating the array. However your solution is accurate (remove the backticks and echo entirely).
              – Patrick
              Jun 13 at 12:21











            • You're right, the result of the backticks gets expanded. I got confused by the entirely useless echo there.
              – TooTea
              Jun 13 at 13:26
















            • This isn't quite accurate. It's not the echo ... which is performing the expansion. It's when going from the backticks to the array. The backticks output is unquoted (which you can't quote without causing other issues, though $() would work), thus it gets expanded before populating the array. However your solution is accurate (remove the backticks and echo entirely).
              – Patrick
              Jun 13 at 12:21











            • You're right, the result of the backticks gets expanded. I got confused by the entirely useless echo there.
              – TooTea
              Jun 13 at 13:26















            This isn't quite accurate. It's not the echo ... which is performing the expansion. It's when going from the backticks to the array. The backticks output is unquoted (which you can't quote without causing other issues, though $() would work), thus it gets expanded before populating the array. However your solution is accurate (remove the backticks and echo entirely).
            – Patrick
            Jun 13 at 12:21





            This isn't quite accurate. It's not the echo ... which is performing the expansion. It's when going from the backticks to the array. The backticks output is unquoted (which you can't quote without causing other issues, though $() would work), thus it gets expanded before populating the array. However your solution is accurate (remove the backticks and echo entirely).
            – Patrick
            Jun 13 at 12:21













            You're right, the result of the backticks gets expanded. I got confused by the entirely useless echo there.
            – TooTea
            Jun 13 at 13:26




            You're right, the result of the backticks gets expanded. I got confused by the entirely useless echo there.
            – TooTea
            Jun 13 at 13:26












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449523%2fwhy-does-bash-convert-to-hidden-file-list-in-current-dir-and-how-to-prevent-i%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?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?