LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-10-2009, 03:35 PM   #1
aoresearch
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
[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.
 
Old 11-10-2009, 03:49 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,319
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
You mean 'array=($(theCommandToRun))'? BTW watch your IFS if you do stuff like that.
 
Old 11-10-2009, 03:55 PM   #3
aoresearch
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
Old 11-10-2009, 04:07 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,319
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
Quote:
Originally Posted by aoresearch View Post
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?
 
Old 11-10-2009, 04:09 PM   #5
mglenney
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 1
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`)
 
Old 11-11-2009, 08:21 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,548
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 02:08 PM
Awk output to bash array? kj6loh Programming 4 09-07-2009 12:36 PM
Bash: Insert output of a commando into an array and compare the values tengblad Programming 2 04-07-2009 03:36 PM
bash: put output from `ls` into an array samel_tvom Programming 18 07-31-2005 12:55 AM


All times are GMT -5. The time now is 07:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration