Use "dollar-single-quotes" to escape shell-sensitive strings

Also called "ANSI-C Quoting" in info:(bash) ANSI-C Quoting.

The escaping rules are a POSIX proposal, and are described in
http://austingroupbugs.net/view.php?id=249. There's a lot of back-and-forth on
the details of escaping of control characters, but we'll be only using a small
subset of the syntax that is common to all proposals and is widely supported.
Unfortunately dash and fish and maybe some other shells do not support it (see
the man page patch for a list).

This allows environment variables to be safely exported using show-environment
and imported into the shell. Shells which do not support this syntax will have
to do something like
    export $(systemctl show-environment|grep -v '=\$')
or whatever is appropriate in their case. I think csh and fish do not support
the A=B syntax anyway, so the change is moot for them.

Fixes #5536.

v2:
- also escape newlines (which currently disallowed in shell values, so this
  doesn't really matter), and tabs (as $'\t'), and ! (as $'!'). This way quoted
  output can be included directly in both interactive and noninteractive bash.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2017-06-19 19:39:43 -04:00
parent 42d3bf86bb
commit 804ee07c13
8 changed files with 134 additions and 33 deletions
+20 -5
View File
@@ -1495,11 +1495,26 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err
<term><command>show-environment</command></term>
<listitem>
<para>Dump the systemd manager environment block. The
environment block will be dumped in straight-forward form
suitable for sourcing into a shell script. This environment
block will be passed to all processes the manager
spawns.</para>
<para>Dump the systemd manager environment block. This is the environment
block that is passed to all processes the manager spawns. The environment
block will be dumped in straight-forward form suitable for sourcing into
most shells. If no special characters or whitespace is present in the variable
values, no escaping is performed, and the assignments have the form
<literal>VARIABLE=value</literal>. If whitespace or characters which have
special meaning to the shell are present, dollar-single-quote escaping is
used, and assignments have the form <literal>VARIABLE=$'value'</literal>.
This syntax is known to be supported by
<citerefentry project='die-net'><refentrytitle>bash</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry project='die-net'><refentrytitle>zsh</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry project='die-net'><refentrytitle>ksh</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
and
<citerefentry project='die-net'><refentrytitle>busybox</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
<citerefentry project='die-net'><refentrytitle>ash</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
but not
<citerefentry project='die-net'><refentrytitle>dash</refentrytitle><manvolnum>1</manvolnum></citerefentry>
or
<citerefentry project='die-net'><refentrytitle>fish</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para>
</listitem>
</varlistentry>
<varlistentry>