Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Created February 3, 2023 01:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmc3/a907eb2d04226db455657f9d0370bf13 to your computer and use it in GitHub Desktop.
Save mattmc3/a907eb2d04226db455657f9d0370bf13 to your computer and use it in GitHub Desktop.
Zsh collect args
# Collect args
# -- traditional, piped|, <redirected
function collect_args {
local data args=()
if [[ ! -t 0 ]]; then
while IFS= read -r data || [[ -n "$data" ]]; do
args+=("$data")
done
fi
printf '%s\n' "$@[@]" "$args[@]"
}
function foo {
set -- "${(f@)$(collect_args "$@")}"
local arg
for arg in "$@"; do
echo "arg: '$arg'"
done
}
arr=(1 2 "" 4)
printf "%s\n" "$arr[@]" | foo x "" y z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment