Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: for loop file order
- X-seq: zsh-users 30759
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: "Mark J. Reed" <markjreed@xxxxxxxxx>
- Cc: Ray Andrews <rayandrews@xxxxxxxxxxx>, zsh-users@xxxxxxx
- Subject: Re: for loop file order
- Date: Mon, 16 Nov 2026 19:11:04 +0100
- Arc-authentication-results: i=1; mx.google.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=lcc1tT6comKVgIh/O5VS+64GcMRfOOCGrGM2RGfF5eM=; fh=dSbh3/wA6u+71agMnlHlAuAoGc/xb/AIxVgLVmkjdnw=; b=KAXTjL63SzENd3Pf41WFWa+bvfoClgl58oAD3OlbFThrpp1i11777EANsmLoqfEzmQ 7QaZginXfgqqbKQVA2OvxAiHk34yqqQEHQF8FfbeaR2aZ5HZRPFf8SP5jhmthROmDwGm 3DEqSHO5cpzFl+YB3aUGpzRKxH6OIKAzXSC6yM9y65qS6conC9+w8n7uAoAOaVkOyvQj hKxGya5zCB0TpqEMPiIcIG1TgP+60wRGoRCr6vs+JN+3FOuu6CTMHWSn/9657pACoExn Du4KGwbV2+ZVPvU4KcRKxDpWw8o8Ydu8er4nsUllrYjaKwf4MhjTfkcEeSBiN9qJIny6 CUAQ==; darn=zsh.org
- Arc-seal: i=1; a=rsa-sha256; t=1794852678; cv=none; d=google.com; s=arc-20260327; b=UHF2JHvY7uXAZgsG9YQ1lPKPrFvVhLgQ3TuzzRLv7Nx4uyqC6RZ6Szf2GcTuUeL8m5 B85cyRfOw5ApNPXd8TlH74MX6LeAwrz41ysHpgO44BeT9wKojdphjQqpy3JBKbD+heU8 qeIk1cSdc5iDtVN0htVFD0fRdG8jxDFyoEtWS7HG98lPpEKLEZOTCM12xHPoG9mixd25 H6SofGNQFsqg7murh88tUmybhAe2ZkkPYCx1NLGvMUJzXZ84m+xS8nEbf3wQ3atLPARE 15MxJDnU/qdUBC/Mk4IpK+SUdtChTWtNO6/rXF6a+D4Ccux88B0lycBfxr0hrzwA3wvn sGIw==
- Archived-at: <https://zsh.org/users/30759>
- In-reply-to: <CAA=-s3zJF0u3Ttocj15Mm78LxfRn0N9_aYux_xOPqHgkGmr8CQ@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <db0d5bca-690d-4b05-b918-503b21e2feda@eastlink.ca> <avq3rAdnVmwcDFNP@chazelas.org> <CAA=-s3yUk1P_1VLpV_Dm3VvLb0tSz4XYNPLTdW3ojgf4jGXqqg@mail.gmail.com> <3ee5aa49-13d6-431f-9fdb-783a343b539a@eastlink.ca> <CAA=-s3zJF0u3Ttocj15Mm78LxfRn0N9_aYux_xOPqHgkGmr8CQ@mail.gmail.com>
On Mon, Nov 16, 2026 at 6:02 PM Mark J. Reed <markjreed@xxxxxxxxx> wrote:
>
> To be clear, you could certainly always do this:
>
>
> for aa in $(ls ...); do
>
>
> in any of the Bournish shells. Unfortunately, that splits the output of ls on whitespace and so will break if any of your filenames have a space inside them. The solution in my message is peculiar to zsh; it uses the ${(flags)"$(...)"} syntax to apply expansion flags to command substitution, which by itself has no place to put them. By quoting the output of ls (which by itself would create one giant string and execute your loop exactly once with aa set to that string - not helpful) and applying the (f) flag to split on newlines, you get the desired result without having to do a while read loop or similar.
This just moves the problem from white space in filenames breaking the
script to newlines in filenames breaking the script, though. I would
recommend never using ls to generate a list of filenames for use in a
script, only for looking at the output with your eyeballs.
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author