Creating shell command strings
Useful for joining multiple arguments into a single string when you want to run them through a sh -c command.
Before:Before:
After:After:
Ugly, but. Reliable. Just how I like it works. :-)
_dumpargs() { printf '[%s]' "$@";"$@" printf '\n';printf '\n' } >&2 dumpargs_dumpargs one two three # [one][two][three] dumpargs_dumpargs one 'two three' # [one][two three] dumpargs_dumpargs one two\ three # [one][two three] dumpargs_dumpargs cat 'aw ful".txt' # [cat][aw ful".txt] dumpargs_dumpargs "$(_shlexjoin cat 'aw ful".txt')" # ['cat' 'aw ful".txt'] # ^ One arg, looks like two ^ _shlexjoin one 'two three' # _shlexjoin() { echo ${*@Q}; } # From above # Output: 'one' 'two three' _shlexjoin_pr one 'two three' # _shlexjoin_pr() { printf ' %q' "$@"# |Output: cutone -b2-;two\ }three _shlexjoin.py_shlexjoin_py one 'two three' # _shlexjoin_py() { python3 -c 'import sys,shlex;print(shlex.join(sys.argv[1:]))' "$@"; } # Output: one two\'two threethree'