Problem with ls in a bash script
Hi
the thing I want to do is saving the output in an array/for loop in a bash script, so I can use them later on... The problem is that the names of the directories contains multiple words, example: "accounting January 2010" I've tried the following: Code:
for i in $(ls /home/karel/accounting) accounting January 2009 as you can see every word of the directory is an entry instead of "accounting January 2009" i get "accounting", "January" and "2009", 3 separate words now this is the problem someone knows a solution? so that every time the $i is called, the name of a directory is shown and not just one word... Thx and happy new-year to you all Dzcharles |
The general answer is: Don't use filenames with spaces.
This illustrates one way around the problem: (I created three files in a directory "names") Code:
[mherring@Ath names]$ ls -l |
quick and dirty
cd /home/karel/accounting for i in * do echo "$i" done |
Quote:
|
spaces in filenames are fine as long as you let the shell do the globbing, like Sethsdads example.
if its inconvenient to change cwd for some reason, you can also say: for i in foo/bar/baz/*; do echo `basename $i`; done often i'll assign the result of basename to a variable and reuse it later. |
Thank you very much
this solves the problem I had :) |
Another solution is to change the field separator temporarily:
Code:
IFS=' |
What about simple
Code:
ls -1 /home/karel/accounting |
All times are GMT -5. The time now is 11:15 AM. |