1SHELL-QUOTE(1) User Contributed Perl Documentation SHELL-QUOTE(1)
2
3
4
6 shell-quote - quote arguments for safe use, unmodified in a shell
7 command
8
10 shell-quote [switch]... arg...
11
13 shell-quote lets you pass arbitrary strings through the shell so that
14 they won't be changed by the shell. This lets you process commands or
15 files with embedded white space or shell globbing characters safely.
16 Here are a few examples.
17
19 ssh preserving args
20 When running a remote command with ssh, ssh doesn't preserve the
21 separate arguments it receives. It just joins them with spaces and
22 passes them to "$SHELL -c". This doesn't work as intended:
23
24 ssh host touch 'hi there' # fails
25
26 It creates 2 files, hi and there. Instead, do this:
27
28 cmd=`shell-quote touch 'hi there'`
29 ssh host "$cmd"
30
31 This gives you just 1 file, hi there.
32
33 process find output
34 It's not ordinarily possible to process an arbitrary list of files
35 output by find with a shell script. Anything you put in $IFS to
36 split up the output could legitimately be in a file's name. Here's
37 how you can do it using shell-quote:
38
39 eval set -- `find -type f -print0 | xargs -0 shell-quote --`
40
41 debug shell scripts
42 shell-quote is better than echo for debugging shell scripts.
43
44 debug() {
45 [ -z "$debug" ] || shell-quote "debug:" "$@"
46 }
47
48 With echo you can't tell the difference between "debug 'foo bar'"
49 and "debug foo bar", but with shell-quote you can.
50
51 save a command for later
52 shell-quote can be used to build up a shell command to run later.
53 Say you want the user to be able to give you switches for a command
54 you're going to run. If you don't want the switches to be re-
55 evaluated by the shell (which is usually a good idea, else there
56 are things the user can't pass through), you can do something like
57 this:
58
59 user_switches=
60 while [ $# != 0 ]
61 do
62 case x$1 in
63 x--pass-through)
64 [ $# -gt 1 ] || die "need an argument for $1"
65 user_switches="$user_switches "`shell-quote -- "$2"`
66 shift;;
67 # process other switches
68 esac
69 shift
70 done
71 # later
72 eval "shell-quote some-command $user_switches my args"
73
75 --debug
76 Turn debugging on.
77
78 --help
79 Show the usage message and die.
80
81 --version
82 Show the version number and exit.
83
85 The code is licensed under the GNU GPL. Check
86 http://www.argon.org/~roderick/ or CPAN for updated versions.
87
89 Roderick Schertler <roderick@argon.org>
90
91
92
93perl v5.36.0 2022-07-22 SHELL-QUOTE(1)