Greg Wooledge wrote:
> You must not have tested it thoroughly. xargs is notoriously poor at
> handling unexpected input. In this case, mismatched quotes will break it:
>
> hobbit:~$ input="Can't work"
> hobbit:~$ printf -- "$input" | xargs -r -n 1 | { readarray -t args;
declare
> -p args; }
> xargs: unmatched single quote; by default quotes are special to xargs
unless
> you use the -0 option
Thanks for pointing out that problem. Setting pipefail and wrapping the
prompt in a while loop seems to be about the best I can do to deal with
that problem.
#!/usr/bin/bash
set -o pipefail
shopt -s lastpipe
function handler {
printf '%s\n' "$@"
}
declare -a args=()
while
read -r -e -p 'parameters: ' input
do
printf -- "$input" \
| xargs -r -n 1 printf -- '%s\n' \
| readarray -t args && break
printf '%s\n' 'error parsing input, please try again'
done
[[ ${#args[@]} -gt 0 ]] && handler "${args[@]}"
---
$ ./test.sh
parameters: Can't work
xargs: unmatched single quote; by default quotes are special to xargs
unless you use the -0 option
error parsing input, please try again
parameters: Can\\'t work
Can't
work
$
It is still far from perfect, but thanks for the improvement.
On Fri, Oct 17, 2025 at 5:30 PM Gregory Bartholomew <
gregory.lee.bartholomew@gmail.com> wrote:
> Oops, better use "| xargs -r -n 1 printf -- '%s\n' |" instead of just "|
> xargs -r -n 1 |".
>
>>