Martin Brisiak
Martin Brisiak

Reputation: 4061

reference array of folders without variable

If i can get actual folder as array thanks to:

myArray=./*

and then I can count elements of this array like this:

${#myArray[@]}

How can I do it without assign it to a variable? Something like this:

${#./*[@]}

Upvotes: 1

Views: 41

Answers (2)

chepner
chepner

Reputation: 531693

bash does not have anonymous arrays. You have to create and populate an array variable, then apply a parameter expansion operator to it. That said, there are alternatives to using an array; @sorontar provides one that is often feasible.

Upvotes: 1

user8017719
user8017719

Reputation:

How about:

set -- ./*;  echo $#

The "positional parameters" get changed with the code above.

Or a more reliable version:

(shopt -s nullglob; set -- ./*; echo $#)

Upvotes: 0

Related Questions