How to create a possibly empty array from filename glob?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question






















  • 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... 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














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?







share|improve this question






















  • 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... 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












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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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... 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















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










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.







share|improve this answer


















  • 2




    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










  • 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











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%2f426816%2fhow-to-create-a-possibly-empty-array-from-filename-glob%23new-answer', 'question_page');

);

Post as a guest






























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.







share|improve this answer


















  • 2




    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










  • 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















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.







share|improve this answer


















  • 2




    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










  • 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













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.







share|improve this answer














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.








share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 27 at 0:10

























answered Feb 26 at 23:01









Jesse_b

10.4k22658




10.4k22658







  • 2




    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










  • 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













  • 2




    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










  • 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








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













 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































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?