How to create a possibly empty array from filename glob?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
With zsh
, it is not difficult to create a possibly empty array from the results of a (possibly null) filename glob. For example:
% pathlist=( /no/such/path/*(N) ); printf -- "%dn" $#pathlist
0
With bash
, however, the closest approximation I can come up with to the code above fails to produce an empty array when the glob is null:
% pathlist=( /no/such/path/* ); printf -- "%dn" $#pathlist[@] )
1
In this case, the pathlist
variable contains a single entry, namely the string "/no/such/path/*".
How can I modify the bash
code so that pathlist
contains exactly one entry for every file (if any) that matches the glob?
bash scripting wildcards array
add a comment |Â
up vote
3
down vote
favorite
With zsh
, it is not difficult to create a possibly empty array from the results of a (possibly null) filename glob. For example:
% pathlist=( /no/such/path/*(N) ); printf -- "%dn" $#pathlist
0
With bash
, however, the closest approximation I can come up with to the code above fails to produce an empty array when the glob is null:
% pathlist=( /no/such/path/* ); printf -- "%dn" $#pathlist[@] )
1
In this case, the pathlist
variable contains a single entry, namely the string "/no/such/path/*".
How can I modify the bash
code so that pathlist
contains exactly one entry for every file (if any) that matches the glob?
bash scripting wildcards array
Possible duplicate of Avoiding errors due to unexpanded asterisk (Chris Down answers your question) or Ignore globs that don't match anything etc...
â don_crissti
Feb 26 at 23:04
1
... and btw, since you're azsh
user... thenullglob
option is azsh
invention, only added years later tobash
â don_crissti
Feb 26 at 23:14
Comments are not for extended discussion; this conversation has been moved to chat.
â Michael Mrozekâ¦
Feb 27 at 17:36
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
With zsh
, it is not difficult to create a possibly empty array from the results of a (possibly null) filename glob. For example:
% pathlist=( /no/such/path/*(N) ); printf -- "%dn" $#pathlist
0
With bash
, however, the closest approximation I can come up with to the code above fails to produce an empty array when the glob is null:
% pathlist=( /no/such/path/* ); printf -- "%dn" $#pathlist[@] )
1
In this case, the pathlist
variable contains a single entry, namely the string "/no/such/path/*".
How can I modify the bash
code so that pathlist
contains exactly one entry for every file (if any) that matches the glob?
bash scripting wildcards array
With zsh
, it is not difficult to create a possibly empty array from the results of a (possibly null) filename glob. For example:
% pathlist=( /no/such/path/*(N) ); printf -- "%dn" $#pathlist
0
With bash
, however, the closest approximation I can come up with to the code above fails to produce an empty array when the glob is null:
% pathlist=( /no/such/path/* ); printf -- "%dn" $#pathlist[@] )
1
In this case, the pathlist
variable contains a single entry, namely the string "/no/such/path/*".
How can I modify the bash
code so that pathlist
contains exactly one entry for every file (if any) that matches the glob?
bash scripting wildcards array
edited Feb 27 at 0:32
Jeff Schaller
31.2k846105
31.2k846105
asked Feb 26 at 22:58
kjo
3,84873358
3,84873358
Possible duplicate of Avoiding errors due to unexpanded asterisk (Chris Down answers your question) or Ignore globs that don't match anything etc...
â don_crissti
Feb 26 at 23:04
1
... and btw, since you're azsh
user... thenullglob
option is azsh
invention, only added years later tobash
â don_crissti
Feb 26 at 23:14
Comments are not for extended discussion; this conversation has been moved to chat.
â Michael Mrozekâ¦
Feb 27 at 17:36
add a comment |Â
Possible duplicate of Avoiding errors due to unexpanded asterisk (Chris Down answers your question) or Ignore globs that don't match anything etc...
â don_crissti
Feb 26 at 23:04
1
... and btw, since you're azsh
user... thenullglob
option is azsh
invention, only added years later tobash
â don_crissti
Feb 26 at 23:14
Comments are not for extended discussion; this conversation has been moved to chat.
â Michael Mrozekâ¦
Feb 27 at 17:36
Possible duplicate of Avoiding errors due to unexpanded asterisk (Chris Down answers your question) or Ignore globs that don't match anything etc...
â don_crissti
Feb 26 at 23:04
Possible duplicate of Avoiding errors due to unexpanded asterisk (Chris Down answers your question) or Ignore globs that don't match anything etc...
â don_crissti
Feb 26 at 23:04
1
1
... and btw, since you're a
zsh
user... the nullglob
option is a zsh
invention, only added years later to bash
â don_crissti
Feb 26 at 23:14
... and btw, since you're a
zsh
user... the nullglob
option is a zsh
invention, only added years later to bash
â don_crissti
Feb 26 at 23:14
Comments are not for extended discussion; this conversation has been moved to chat.
â Michael Mrozekâ¦
Feb 27 at 17:36
Comments are not for extended discussion; this conversation has been moved to chat.
â Michael Mrozekâ¦
Feb 27 at 17:36
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
You need to use the shell option nullglob
#!/bin/bash
shopt -s nullglob
pathlist=( /no/such/path/* ); printf -- "%dn" "$#pathlist[@]" )
shopt -u nullglob
The Bash manual says the following about the nullglob
shell option:
If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
2
notedeclare -p pathlist
is a nice way to dump an array variable.
â glenn jackman
Feb 26 at 23:30
@glennjackman: Where would that fit into the answer?
â Jesse_b
Feb 26 at 23:35
Instead of the printf...
â Jeff Schaller
Feb 26 at 23:56
OP is using printf to simply print the number of elements in the array though, not a declare statement for it...If anything it would be better written as simplyecho "$#pathlist[@]"
â Jesse_b
Feb 27 at 0:09
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
You need to use the shell option nullglob
#!/bin/bash
shopt -s nullglob
pathlist=( /no/such/path/* ); printf -- "%dn" "$#pathlist[@]" )
shopt -u nullglob
The Bash manual says the following about the nullglob
shell option:
If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
2
notedeclare -p pathlist
is a nice way to dump an array variable.
â glenn jackman
Feb 26 at 23:30
@glennjackman: Where would that fit into the answer?
â Jesse_b
Feb 26 at 23:35
Instead of the printf...
â Jeff Schaller
Feb 26 at 23:56
OP is using printf to simply print the number of elements in the array though, not a declare statement for it...If anything it would be better written as simplyecho "$#pathlist[@]"
â Jesse_b
Feb 27 at 0:09
add a comment |Â
up vote
7
down vote
accepted
You need to use the shell option nullglob
#!/bin/bash
shopt -s nullglob
pathlist=( /no/such/path/* ); printf -- "%dn" "$#pathlist[@]" )
shopt -u nullglob
The Bash manual says the following about the nullglob
shell option:
If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
2
notedeclare -p pathlist
is a nice way to dump an array variable.
â glenn jackman
Feb 26 at 23:30
@glennjackman: Where would that fit into the answer?
â Jesse_b
Feb 26 at 23:35
Instead of the printf...
â Jeff Schaller
Feb 26 at 23:56
OP is using printf to simply print the number of elements in the array though, not a declare statement for it...If anything it would be better written as simplyecho "$#pathlist[@]"
â Jesse_b
Feb 27 at 0:09
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
You need to use the shell option nullglob
#!/bin/bash
shopt -s nullglob
pathlist=( /no/such/path/* ); printf -- "%dn" "$#pathlist[@]" )
shopt -u nullglob
The Bash manual says the following about the nullglob
shell option:
If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
You need to use the shell option nullglob
#!/bin/bash
shopt -s nullglob
pathlist=( /no/such/path/* ); printf -- "%dn" "$#pathlist[@]" )
shopt -u nullglob
The Bash manual says the following about the nullglob
shell option:
If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
edited Feb 27 at 0:10
answered Feb 26 at 23:01
Jesse_b
10.4k22658
10.4k22658
2
notedeclare -p pathlist
is a nice way to dump an array variable.
â glenn jackman
Feb 26 at 23:30
@glennjackman: Where would that fit into the answer?
â Jesse_b
Feb 26 at 23:35
Instead of the printf...
â Jeff Schaller
Feb 26 at 23:56
OP is using printf to simply print the number of elements in the array though, not a declare statement for it...If anything it would be better written as simplyecho "$#pathlist[@]"
â Jesse_b
Feb 27 at 0:09
add a comment |Â
2
notedeclare -p pathlist
is a nice way to dump an array variable.
â glenn jackman
Feb 26 at 23:30
@glennjackman: Where would that fit into the answer?
â Jesse_b
Feb 26 at 23:35
Instead of the printf...
â Jeff Schaller
Feb 26 at 23:56
OP is using printf to simply print the number of elements in the array though, not a declare statement for it...If anything it would be better written as simplyecho "$#pathlist[@]"
â Jesse_b
Feb 27 at 0:09
2
2
note
declare -p pathlist
is a nice way to dump an array variable.â glenn jackman
Feb 26 at 23:30
note
declare -p pathlist
is a nice way to dump an array variable.â glenn jackman
Feb 26 at 23:30
@glennjackman: Where would that fit into the answer?
â Jesse_b
Feb 26 at 23:35
@glennjackman: Where would that fit into the answer?
â Jesse_b
Feb 26 at 23:35
Instead of the printf...
â Jeff Schaller
Feb 26 at 23:56
Instead of the printf...
â Jeff Schaller
Feb 26 at 23:56
OP is using printf to simply print the number of elements in the array though, not a declare statement for it...If anything it would be better written as simply
echo "$#pathlist[@]"
â Jesse_b
Feb 27 at 0:09
OP is using printf to simply print the number of elements in the array though, not a declare statement for it...If anything it would be better written as simply
echo "$#pathlist[@]"
â Jesse_b
Feb 27 at 0:09
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%2f426816%2fhow-to-create-a-possibly-empty-array-from-filename-glob%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
Possible duplicate of Avoiding errors due to unexpanded asterisk (Chris Down answers your question) or Ignore globs that don't match anything etc...
â don_crissti
Feb 26 at 23:04
1
... and btw, since you're a
zsh
user... thenullglob
option is azsh
invention, only added years later tobash
â don_crissti
Feb 26 at 23:14
Comments are not for extended discussion; this conversation has been moved to chat.
â Michael Mrozekâ¦
Feb 27 at 17:36