[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash question

* Stephan Sauerburger
> This is a general shell-scripting question:
> In a for loop which runs through all files, as in:
> for file in `ls`
> do
>   #stuff
> done
> How do I have it make sure it iterates file-by-file? The following example, to play all
> mp3s in the current directory:
> for file in `ls`
> do
>   mpg123 $file
> done
> ...will do just a fine job, so long as none of the file names have any
> spaces in them. If my directory contained:

The simple and correct solution to just this question is not to use
`ls`, but *:

 for file in *
   mpg123 "$file"

Note also that you need to quote $file. However, you get anyway into
all sorts of trouble if there are other files, not to say directories
in your directory.

A very failsafe variant is to use find:

find . -type f -print0 | xargs -0 mpg123

 Jon Haugsand, Jon-H.Haugsand@norges-bank.no

Reply to: