Created
August 20, 2022 12:54
-
-
Save mattmc3/08600d86ef3cbcb96cf998b53fb4d8f1 to your computer and use it in GitHub Desktop.
Zsh - append piped/redirected input to arg array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example to show how to append piped/redirected input to arg array | |
# | |
# More info: | |
# `man test` (lookup -t flag) | |
# `man zshbuiltins` (lookup set builtin) | |
# | |
# piped_input_example | |
# piped_input_example --flag arg | |
# piped_input_example --opt1 -x -y -z param1 param2 <<< "zzz" | |
# echo "abc" | piped_input_example --opt1 -x -y -z param1 param2 | |
# printf "%s\n" abc def | piped_input_example --opt1 -x -y -z param1 param2 | |
function piped_input_example { | |
local piped=() | |
if [[ ! -t 0 ]]; then | |
# handle both <redirected or piped| input | |
local data | |
while IFS= read -r data || [[ -n "$data" ]]; do | |
piped+=($data) | |
done | |
fi | |
echo "regular args ($#): $@" | |
echo "piped args ($#piped): $piped" | |
echo "setting new arg array..." | |
(( $#piped )) && set -- "$@" "$piped[@]" | |
echo "new arg array ($#): $@" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment