Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 255
Jordin Youssef Avatar asked Sep 19 '13 01:09

Jordin Youssef


People also ask

How do I index an array in Bash?

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.

How do you use ls in Bash?

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.


1 Answers

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.

like image 144
Aaron Okano Avatar answered Sep 28 '22 01:09

Aaron Okano