LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [Bash] array `ls` output (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bbash%5D-array-%60ls%60-output-768252/)

aoresearch 11-10-2009 03:35 PM

[Bash] array `ls` output
 
I'm having a problem with arraying the output of the list function, ls, which is strange because I just had the capability earlier today.

bash-3.2$ array=$(`ls`)
bash: asat: command not found


I simply want to create an array that has file listings from the current directory. Any suggestions?

Thanks.

unSpawn 11-10-2009 03:49 PM

You mean 'array=($(theCommandToRun))'? BTW watch your IFS if you do stuff like that.

aoresearch 11-10-2009 03:55 PM

Correct. None of the file listings currently have white spaces, so I don't think I'm having a problem related to IFS. I thought I was performing a pretty basic operation, and it was working until about an hour ago.

unSpawn 11-10-2009 04:07 PM

Quote:

Originally Posted by aoresearch (Post 3752073)
None of the file listings currently have white spaces, so I don't think I'm having a problem related to IFS.

Ponder preventing versus curing?

mglenney 11-10-2009 04:09 PM

I don't know why it worked for you before. Doesn't seem like it should have. But you can accomplish what you want using:

Code:

array=($(ls))
or

Code:

array=(`ls`)

catkin 11-11-2009 08:21 AM

ls may be aliased. It often is. To be sure of getting the standard command use /bin/ls or \ls.

ls lists files across the page. To get one per line use ls -1 (that's a number 1).

The robust way to read file names into shell variables is detailed here as
Code:

find . -type f -print0 | while IFS= read -r -d '' filename; do
  ...
done

This can, of course, be adapted to load an array, one member per loop pass.


All times are GMT -5. The time now is 04:15 PM.