I want to get a list of files and then read the results into an array where each array element corresponds to a file name. Is this possible?
How to Echo a Bash Array? To echo an array, use the format echo ${Array[0]}. Array is your array name, and 0 is the index or the key if you are echoing an associative array. You can also use @ or * symbols instead of an index to print the entire array.
Don't use ls
, it's not intended for this purpose. Use globbing.
shopt -s nullglob array=(*) array2=(file*) array3=(dir/*)
The nullglob
option causes the array to be empty if there are no matches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With