Is there any advantage in specifying './' in a for loop using a glob?
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
add a comment |
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
1
The usual point to./*
is that it ensures that names that start with-
aren't treated as options.
– Charles Duffy
Feb 25 at 18:16
add a comment |
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
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
bash shell wildcards
edited Feb 25 at 14:22
terdon♦
131k32258437
131k32258437
asked Feb 25 at 13:32
Frédéric MahéFrédéric Mahé
485
485
1
The usual point to./*
is that it ensures that names that start with-
aren't treated as options.
– Charles Duffy
Feb 25 at 18:16
add a comment |
1
The usual point to./*
is that it ensures that names that start with-
aren't treated as options.
– Charles Duffy
Feb 25 at 18:16
1
1
The usual point to
./*
is that it ensures that names that start with -
aren't treated as options.– Charles Duffy
Feb 25 at 18:16
The usual point to
./*
is that it ensures that names that start with -
aren't treated as options.– Charles Duffy
Feb 25 at 18:16
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
edited Feb 25 at 13:55
answered Feb 25 at 13:41
Jeff SchallerJeff Schaller
42.9k1159137
42.9k1159137
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
The usual point to
./*
is that it ensures that names that start with-
aren't treated as options.– Charles Duffy
Feb 25 at 18:16