Post History
Populate an array of the file names and then print how many entries are in the array: $ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" ) 124 That will work correctly even if your fi...
Answer
#3: Post edited
- Populate an array of the file names and then print how many entries are in the array:
- $ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" )
- 124
That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`. I'm using a subshell to restrict the environment that `shopt -s nullglob` applies to - if you don't care about that then you don't need a subshell.
- Populate an array of the file names and then print how many entries are in the array:
- $ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" )
- 124
- That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`. I'm using a subshell to restrict the environment that `shopt -s nullglob` applies to - if you don't care about that then you don't need a subshell.
- Note that the above includes both files and directories (which are, of course, a type of file) - if you want to exclude directories then you need a different solution.
#2: Post edited
- Populate an array of the file names and then print how many entries are in the array:
$ files=( * )$ echo "${#files[@]}"- 124
That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`.
- Populate an array of the file names and then print how many entries are in the array:
- $ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" )
- 124
- That will work correctly even if your file names contain newlines, unlike anything piped to `wc -l`. I'm using a subshell to restrict the environment that `shopt -s nullglob` applies to - if you don't care about that then you don't need a subshell.