arraysbashshellls

How do I assign ls to an array in Linux Bash?


array=${ls -d */}
echo ${array[@]}  

I have three directories: ww ee qq. I want them in an array and then print the array.


Solution

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