Is there any advantage in specifying './' in a for loop using a glob?Why is printf better than echo?Handling...

How to create a label containing values from different layers in QGIS

Does an Eldritch Knight's Weapon Bond protect him from losing his weapon to a Telekinesis spell?

Why didn't the 2019 Oscars have a host?

Single-row INSERT...SELECT much slower than separate SELECT

Could a warlock use the One with Shadows warlock invocation to turn invisible, and then move while staying invisible?

Does the US government have any planning in place to ensure there's no shortages of food, fuel, steel and other commodities?

What makes papers publishable in top-tier journals?

Why did Luke use his left hand to shoot?

Do authors have to be politically correct in article-writing?

Is there a verb that means to inject with poison?

Is there a way to not have to poll the UART of an AVR?

How do you get out of your own psychology to write characters?

How vim overwrites readonly mode?

I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."

Cat is tipping over bed-side lamps during the night

Illustrator to chemdraw

Can you determine if focus is sharp without diopter adjustment if your sight is imperfect?

Non-Cancer terminal illness that can affect young (age 10-13) girls?

Can a player sacrifice a creature after declaring that creature as blocker while taking lethal damage?

A question about partitioning positivie integers into finitely many arithmetic progresions

Article. The word "Respect"

How is this property called for mod?

Taking headphones when quitting job

How to write cases in LaTeX?



Is there any advantage in specifying './' in a for loop using a glob?


Why is printf better than echo?Handling names with leading dash in bash shellExecuting a shell command from PHP with shell_execConcatenate files in multiple matching subdirectoriesIs there any advantage on using ksh over zsh?Shell glob expansion after loop variable substitutionScrabble helper in bashfor loop glob mishapsGlob for matching everything but . andBash pattern to match directories whose names begin with a dot (period), by being “explicit”, instead of using “shopt -s dotglob”?When do we use single and when double square brackets […] vs [[…]]?Running multiple command against files matching a brace+glob pattern without repeating it













8















I was under the impression it could be safer to use ./*.fastq when searching for files ending with .fastq. For example, ./ would prevent capturing the file .fastq. This is obviously wrong, as shown in the example below:



TMP_DIR=$(mktemp --directory)
mkdir -p ${TMP_DIR}
(cd ${TMP_DIR}
touch {a,b,c,}.fastq
ls -a
echo ""

echo "# match all:"
for f in *.fastq ; do
echo "${f}"
done
echo ""

echo "# with ./:"
for f in ./*.fastq ; do
echo "${f}"
done
)
rm -rf ${TMP_DIR}


.
..
a.fastq
b.fastq
c.fastq
.fastq

# match all:
a.fastq
b.fastq
c.fastq

# with ./:
./a.fastq
./b.fastq
./c.fastq


Neither *.fastq nor ./*.fastq match the file .fastq. So I wonder now, is there any point using ./*.fastq here, or ./* in general?










share|improve this question









New contributor




Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • The usual point to ./* is that it ensures that names that start with - aren't treated as options.

    – Charles Duffy
    2 hours ago


















8















I was under the impression it could be safer to use ./*.fastq when searching for files ending with .fastq. For example, ./ would prevent capturing the file .fastq. This is obviously wrong, as shown in the example below:



TMP_DIR=$(mktemp --directory)
mkdir -p ${TMP_DIR}
(cd ${TMP_DIR}
touch {a,b,c,}.fastq
ls -a
echo ""

echo "# match all:"
for f in *.fastq ; do
echo "${f}"
done
echo ""

echo "# with ./:"
for f in ./*.fastq ; do
echo "${f}"
done
)
rm -rf ${TMP_DIR}


.
..
a.fastq
b.fastq
c.fastq
.fastq

# match all:
a.fastq
b.fastq
c.fastq

# with ./:
./a.fastq
./b.fastq
./c.fastq


Neither *.fastq nor ./*.fastq match the file .fastq. So I wonder now, is there any point using ./*.fastq here, or ./* in general?










share|improve this question









New contributor




Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • The usual point to ./* is that it ensures that names that start with - aren't treated as options.

    – Charles Duffy
    2 hours ago
















8












8








8








I was under the impression it could be safer to use ./*.fastq when searching for files ending with .fastq. For example, ./ would prevent capturing the file .fastq. This is obviously wrong, as shown in the example below:



TMP_DIR=$(mktemp --directory)
mkdir -p ${TMP_DIR}
(cd ${TMP_DIR}
touch {a,b,c,}.fastq
ls -a
echo ""

echo "# match all:"
for f in *.fastq ; do
echo "${f}"
done
echo ""

echo "# with ./:"
for f in ./*.fastq ; do
echo "${f}"
done
)
rm -rf ${TMP_DIR}


.
..
a.fastq
b.fastq
c.fastq
.fastq

# match all:
a.fastq
b.fastq
c.fastq

# with ./:
./a.fastq
./b.fastq
./c.fastq


Neither *.fastq nor ./*.fastq match the file .fastq. So I wonder now, is there any point using ./*.fastq here, or ./* in general?










share|improve this question









New contributor




Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I was under the impression it could be safer to use ./*.fastq when searching for files ending with .fastq. For example, ./ would prevent capturing the file .fastq. This is obviously wrong, as shown in the example below:



TMP_DIR=$(mktemp --directory)
mkdir -p ${TMP_DIR}
(cd ${TMP_DIR}
touch {a,b,c,}.fastq
ls -a
echo ""

echo "# match all:"
for f in *.fastq ; do
echo "${f}"
done
echo ""

echo "# with ./:"
for f in ./*.fastq ; do
echo "${f}"
done
)
rm -rf ${TMP_DIR}


.
..
a.fastq
b.fastq
c.fastq
.fastq

# match all:
a.fastq
b.fastq
c.fastq

# with ./:
./a.fastq
./b.fastq
./c.fastq


Neither *.fastq nor ./*.fastq match the file .fastq. So I wonder now, is there any point using ./*.fastq here, or ./* in general?







bash shell wildcards






share|improve this question









New contributor




Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 6 hours ago









terdon

131k32258436




131k32258436






New contributor




Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









Frédéric MahéFrédéric Mahé

413




413




New contributor




Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Frédéric Mahé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • The usual point to ./* is that it ensures that names that start with - aren't treated as options.

    – Charles Duffy
    2 hours ago





















  • The usual point to ./* is that it ensures that names that start with - aren't treated as options.

    – Charles Duffy
    2 hours ago



















The usual point to ./* is that it ensures that names that start with - aren't treated as options.

– Charles Duffy
2 hours ago







The usual point to ./* is that it ensures that names that start with - aren't treated as options.

– Charles Duffy
2 hours ago












1 Answer
1






active

oldest

votes


















12














That's initially surprising wildcard behavior, since the description for the * wildcard character says:




Matches any string, including the null string.




... until you realize that period is slightly special when it's the first character of a filename. The introductory text in 3.5.8 Filename Expansion says it:




When a pattern is used for filename expansion, the character ‘.’ at the start of a filename or immediately following a slash must be matched explicitly, unless the shell option dotglob is set.




The "usage pattern" of prefixing wildcards with ./ is useful for Handling names with leading dash in bash shell, as steeldriver commented. It has no effect on the wildcard / filename expansion, but makes it safer/easier to handle the filenames when you refer to them, if they begin with characters that those programs might misinterpret as options. For example:



# I want a file named `-n`
$ touch -n
touch: invalid option -- 'n'
Try 'touch --help' for more information.
$ touch -- -n
### ok
$ touch ./-n
### ok


... and now that I have a file named -n, if I happen to loop over it with a wildcard:



for file in *n
do
echo "$file"
done


... I get no output!



But if I prefix the wildcard with ./,



for file in ./*n
do
echo "$file"
done
./-n


... I see the filename.



This is a simple example for demonstration purposes; see also Why is printf better than echo? for this reason and others. Other utilities will get tripped up by other options, so it's better to present the filenames to the utilities as safely as possible. If you don't prefix the wildcard to "escape" filenames, you'd have to "protect" your utilities in other ways; one common one is to signal the end of options with --, for example:



for file in *n
do
mv -- "$file" backup/"$file"
done


... which will safely pass the -n filename to mv (as seen under set -x):



mv -- -n backup/-n





share|improve this answer

























    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Frédéric Mahé is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502884%2fis-there-any-advantage-in-specifying-in-a-for-loop-using-a-glob%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    That's initially surprising wildcard behavior, since the description for the * wildcard character says:




    Matches any string, including the null string.




    ... until you realize that period is slightly special when it's the first character of a filename. The introductory text in 3.5.8 Filename Expansion says it:




    When a pattern is used for filename expansion, the character ‘.’ at the start of a filename or immediately following a slash must be matched explicitly, unless the shell option dotglob is set.




    The "usage pattern" of prefixing wildcards with ./ is useful for Handling names with leading dash in bash shell, as steeldriver commented. It has no effect on the wildcard / filename expansion, but makes it safer/easier to handle the filenames when you refer to them, if they begin with characters that those programs might misinterpret as options. For example:



    # I want a file named `-n`
    $ touch -n
    touch: invalid option -- 'n'
    Try 'touch --help' for more information.
    $ touch -- -n
    ### ok
    $ touch ./-n
    ### ok


    ... and now that I have a file named -n, if I happen to loop over it with a wildcard:



    for file in *n
    do
    echo "$file"
    done


    ... I get no output!



    But if I prefix the wildcard with ./,



    for file in ./*n
    do
    echo "$file"
    done
    ./-n


    ... I see the filename.



    This is a simple example for demonstration purposes; see also Why is printf better than echo? for this reason and others. Other utilities will get tripped up by other options, so it's better to present the filenames to the utilities as safely as possible. If you don't prefix the wildcard to "escape" filenames, you'd have to "protect" your utilities in other ways; one common one is to signal the end of options with --, for example:



    for file in *n
    do
    mv -- "$file" backup/"$file"
    done


    ... which will safely pass the -n filename to mv (as seen under set -x):



    mv -- -n backup/-n





    share|improve this answer






























      12














      That's initially surprising wildcard behavior, since the description for the * wildcard character says:




      Matches any string, including the null string.




      ... until you realize that period is slightly special when it's the first character of a filename. The introductory text in 3.5.8 Filename Expansion says it:




      When a pattern is used for filename expansion, the character ‘.’ at the start of a filename or immediately following a slash must be matched explicitly, unless the shell option dotglob is set.




      The "usage pattern" of prefixing wildcards with ./ is useful for Handling names with leading dash in bash shell, as steeldriver commented. It has no effect on the wildcard / filename expansion, but makes it safer/easier to handle the filenames when you refer to them, if they begin with characters that those programs might misinterpret as options. For example:



      # I want a file named `-n`
      $ touch -n
      touch: invalid option -- 'n'
      Try 'touch --help' for more information.
      $ touch -- -n
      ### ok
      $ touch ./-n
      ### ok


      ... and now that I have a file named -n, if I happen to loop over it with a wildcard:



      for file in *n
      do
      echo "$file"
      done


      ... I get no output!



      But if I prefix the wildcard with ./,



      for file in ./*n
      do
      echo "$file"
      done
      ./-n


      ... I see the filename.



      This is a simple example for demonstration purposes; see also Why is printf better than echo? for this reason and others. Other utilities will get tripped up by other options, so it's better to present the filenames to the utilities as safely as possible. If you don't prefix the wildcard to "escape" filenames, you'd have to "protect" your utilities in other ways; one common one is to signal the end of options with --, for example:



      for file in *n
      do
      mv -- "$file" backup/"$file"
      done


      ... which will safely pass the -n filename to mv (as seen under set -x):



      mv -- -n backup/-n





      share|improve this answer




























        12












        12








        12







        That's initially surprising wildcard behavior, since the description for the * wildcard character says:




        Matches any string, including the null string.




        ... until you realize that period is slightly special when it's the first character of a filename. The introductory text in 3.5.8 Filename Expansion says it:




        When a pattern is used for filename expansion, the character ‘.’ at the start of a filename or immediately following a slash must be matched explicitly, unless the shell option dotglob is set.




        The "usage pattern" of prefixing wildcards with ./ is useful for Handling names with leading dash in bash shell, as steeldriver commented. It has no effect on the wildcard / filename expansion, but makes it safer/easier to handle the filenames when you refer to them, if they begin with characters that those programs might misinterpret as options. For example:



        # I want a file named `-n`
        $ touch -n
        touch: invalid option -- 'n'
        Try 'touch --help' for more information.
        $ touch -- -n
        ### ok
        $ touch ./-n
        ### ok


        ... and now that I have a file named -n, if I happen to loop over it with a wildcard:



        for file in *n
        do
        echo "$file"
        done


        ... I get no output!



        But if I prefix the wildcard with ./,



        for file in ./*n
        do
        echo "$file"
        done
        ./-n


        ... I see the filename.



        This is a simple example for demonstration purposes; see also Why is printf better than echo? for this reason and others. Other utilities will get tripped up by other options, so it's better to present the filenames to the utilities as safely as possible. If you don't prefix the wildcard to "escape" filenames, you'd have to "protect" your utilities in other ways; one common one is to signal the end of options with --, for example:



        for file in *n
        do
        mv -- "$file" backup/"$file"
        done


        ... which will safely pass the -n filename to mv (as seen under set -x):



        mv -- -n backup/-n





        share|improve this answer















        That's initially surprising wildcard behavior, since the description for the * wildcard character says:




        Matches any string, including the null string.




        ... until you realize that period is slightly special when it's the first character of a filename. The introductory text in 3.5.8 Filename Expansion says it:




        When a pattern is used for filename expansion, the character ‘.’ at the start of a filename or immediately following a slash must be matched explicitly, unless the shell option dotglob is set.




        The "usage pattern" of prefixing wildcards with ./ is useful for Handling names with leading dash in bash shell, as steeldriver commented. It has no effect on the wildcard / filename expansion, but makes it safer/easier to handle the filenames when you refer to them, if they begin with characters that those programs might misinterpret as options. For example:



        # I want a file named `-n`
        $ touch -n
        touch: invalid option -- 'n'
        Try 'touch --help' for more information.
        $ touch -- -n
        ### ok
        $ touch ./-n
        ### ok


        ... and now that I have a file named -n, if I happen to loop over it with a wildcard:



        for file in *n
        do
        echo "$file"
        done


        ... I get no output!



        But if I prefix the wildcard with ./,



        for file in ./*n
        do
        echo "$file"
        done
        ./-n


        ... I see the filename.



        This is a simple example for demonstration purposes; see also Why is printf better than echo? for this reason and others. Other utilities will get tripped up by other options, so it's better to present the filenames to the utilities as safely as possible. If you don't prefix the wildcard to "escape" filenames, you'd have to "protect" your utilities in other ways; one common one is to signal the end of options with --, for example:



        for file in *n
        do
        mv -- "$file" backup/"$file"
        done


        ... which will safely pass the -n filename to mv (as seen under set -x):



        mv -- -n backup/-n






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 6 hours ago

























        answered 6 hours ago









        Jeff SchallerJeff Schaller

        42.2k1156134




        42.2k1156134






















            Frédéric Mahé is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Frédéric Mahé is a new contributor. Be nice, and check out our Code of Conduct.













            Frédéric Mahé is a new contributor. Be nice, and check out our Code of Conduct.












            Frédéric Mahé is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502884%2fis-there-any-advantage-in-specifying-in-a-for-loop-using-a-glob%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Cannot install PyQt5 The Next CEO of Stack OverflowCannot install tcpreplay 3.4.4cannot...

            Kapp-Putsch Acontecimentos | Outros artigos | Menu de navegação

            Why did early computer designers eschew integers? The Next CEO of Stack OverflowWhat register...