array=${ls -d */} echo ${array[@]}
I have three directories: ww
ee
qq
. I want them in an array and then print the array.
To find the index of specified element in an array in Bash, use For loop to iterate over the index of this array. In the for loop body, check if the current element is equal to the specified element. If there is a match, we may break the For loop and we have found index of first occurrence of specified element.
First Command: ls The ls command lists the files in your current directory (ls is short for "list"). Try it now by typing ls, then hitting <enter>. Since there is nothing to show, ls shows nothing, and bash simply gives you the next prompt ($), indicating that it is ready for a new command.
It would be this
array=($(ls -d */))
EDIT: See Gordon Davisson's solution for a more general answer (i.e. if your filenames contain special characters). This answer is merely a syntax correction.
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