SHELL-QUOTE(1p)
π shell-quote β like, quote your args so the shell doesn't touch them, ok??
okay bestie, so this is shell-quote from the Perl user-contributed docs section, manual page SHELL-QUOTE(1p), and it is literally the moment. It's a tiny little tool with main character energy and it solves a problem you have definitely cried about before. Let's get into it. π«Ά
β¨ NAME β¨
shell-quote β quote arguments so they can be used safely, unmodified, inside a shell command. no cap. π
β¨ SYNOPSIS β¨
shell-quote [switch]... arg...
that's it. that's the whole vibe. switches, then args. periodt. π
β¨ DESCRIPTION β¨
so the tea is: shell-quote lets you pass arbitrary strings through the shell so the shell won't go messing with them. iconic behavior honestly. which means you can safely process commands or files that have embedded white space or shell globbing characters in them β you know, the ones that normally ruin your entire afternoon. π
here are some examples, and honestly? they ate. π₯
π¦ EXAMPLES π¦
πΈ ssh preserving args
ngl this is the classic. when you run a remote command with ssh, ssh does not preserve the separate arguments it receives. it just joins them with spaces and passes them to "$SHELL -c". so this does NOT work the way you want:
ssh host touch 'hi there' # fails
bestie it creates 2 files: hi and there. not one file. two. π instead, do this:
cmd=`shell-quote touch 'hi there'`
ssh host "$cmd"
and now you get just 1 file: hi there. slay. π
πΈ process find output
so it's not ordinarily possible to process an arbitrary list of files output by find with a shell script. and the reason is unhinged: anything you put in $IFS to split up the output could legitimately be in a file's name. like. every separator is a valid filename. rude of the universe fr fr. π
here's how you can do it using shell-quote:
eval set -- `find -type f -print0 | xargs -0 shell-quote --`
there we go. that's the one. β¨
πΈ debug shell scripts
shell-quote is better than echo for debugging shell scripts:
debug() {
[ -z "$debug" ] || shell-quote "debug:" "$@"
}
the way that with echo you literally can't tell the difference between "debug 'foo bar'" and "debug foo bar", but with shell-quote you can???? yeah. that's the whole point. highkey obsessed. π₯°
πΈ save a command for later
shell-quote can be used to build up a shell command to run later. say you want the user to be able to give you switches for a command you're going to run. if you don't want the switches to be re-evaluated by the shell (which is usually a good idea, else there are things the user can't pass through), you can do something like this:
user_switches=
while [ $# != 0 ]
do
case x$1 in
x--pass-through)
[ $# -gt 1 ] || die "need an argument for $1"
user_switches="$user_switches "`shell-quote -- "$2"`
shift;;
# process other switches
esac
shift
done
# later
eval "shell-quote some-command $user_switches my args"
look at that. beautiful. a little masterpiece. π¨π
π OPTIONS π
--debugβ turn debugging on. π (we love a paper trail)--helpβ show the usage message and die. dramatic exit, honestly iconic π--versionβ show the version number and exit. no dying here, just leaving β¨
π AVAILABILITY π
the code is licensed under the GNU GPL. check http://www.argon.org/~roderick/ or CPAN for updated versions. go get your fresh copy bestie, don't be running stale code π«Ά
π AUTHOR π
Roderick Schertler <roderick@argon.org> β the legend, the moment, the blueprint π
so yeah, that's shell-quote, documented for the perl v5.36.0 situation, dated 2022-10-22 (yes it's giving vintage but it still works, it's built different π
). now go forth and quote your args. ily, stay safe out there β¨π¦πΈ
π¦ where this page comes from
- ubuntu noble: libstring-shellquote-perl 1.04-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libstring-shellquote-perl 1.04-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libstring-shellquote-perl 1.04-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms