help-bash
[Top][All Lists]
Advanced

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

Re: Loop with files coming through sorted by name


From: Dennis Williamson
Subject: Re: Loop with files coming through sorted by name
Date: Fri, 15 Oct 2021 21:50:32 -0500

On Fri, Oct 15, 2021, 9:47 PM Dennis Williamson <dennistwilliamson@gmail.com>
wrote:

>
>
> On Fri, Oct 15, 2021, 9:07 PM tolugboji via <help-bash@gnu.org> wrote:
>
>> I have a list of image files and want to pass them in a loop with files
>> coming through sorted by name.
>>
>> Have seen this approach
>>
>> for
>>
>> filename
>>
>> in
>>
>> `ls | sort -n`
>>
>> Are backticks still a good way to do this, as currently things are
>> shifting towards $().
>
>
> Bash sorts filenames lexically when using globs.
>
> for file in *
>
> does what you want without needing to use the sort utility and without
> involving ls. It works even if filenames contain spaces or newlines - just
> make sure you quote the variable when you're using its value
>
> You don't need command substitution as a result, but when you do always
> use $() and never use backticks.
>

Oh, by the way, sort -n sorts numerically - not by name. If that's
actually what you're doing then you should use the find ... print0 |
sort-zn form.

>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]