Building Command Lines in zsh

I love zsh, but sometimes it's a tough love. Building command lines in strings is very hard because zsh doesn't want you hurting yourself on argument splitting like bash, so I just banged my head on that for like 15 minutes before I quit being dumb. Building them in arrays is super trivial:

% a=(args foo bar)
% b=$(ls)
% a=($a $b) # expands $a, adds $b to the end as a single argument
% $a
1=foo
2=bar
3=file1
file2

Note that arg 3 is multiple words/lines, but one arg. That's a nightmare to express in bash, but makes perfect sense at the exec level used by "real" (non-shell) programming languages.

args is a very useful debugging script I've had in some form since the '80s:

#!/bin/zsh
i=1
while [[ $# -gt 0 ]]; do
    echo "$i=$1"
    let i=i+1
    shift
done

Leave a Reply