SHELL-QUOTE(1p)

πŸ“¦
version 1.04 Β· 4 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’… 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