Why does bash convert .* to hidden file list in current dir and how to prevent it from doing it?
Clash 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?
bash
add a comment |Â
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?
bash
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
add a comment |Â
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?
bash
# 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?
bash
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
add a comment |Â
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
add a comment |Â
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.
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 bybash
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 bybash
" -- This is incorrect. It will not.
â Patrick
Jun 13 at 13:37
add a comment |Â
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
add a comment |Â
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.
This isn't quite accurate. It's not theecho ...
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
add a comment |Â
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.
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 bybash
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 bybash
" -- This is incorrect. It will not.
â Patrick
Jun 13 at 13:37
add a comment |Â
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.
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 bybash
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 bybash
" -- This is incorrect. It will not.
â Patrick
Jun 13 at 13:37
add a comment |Â
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.
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.
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 bybash
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 bybash
" -- This is incorrect. It will not.
â Patrick
Jun 13 at 13:37
add a comment |Â
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 bybash
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 bybash
" -- 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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Jun 13 at 13:29
ilkkachu
47.5k668130
47.5k668130
add a comment |Â
add a comment |Â
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.
This isn't quite accurate. It's not theecho ...
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
add a comment |Â
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.
This isn't quite accurate. It's not theecho ...
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
add a comment |Â
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.
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.
edited Jun 13 at 13:29
answered Jun 13 at 12:02
TooTea
2715
2715
This isn't quite accurate. It's not theecho ...
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
add a comment |Â
This isn't quite accurate. It's not theecho ...
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
add a comment |Â
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
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
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
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
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
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