Python's standard library has a shlex.quote function that takes a string and returns one that is guaranteed to be interpreted as that same string by Unix shells. This is achieved by putting the string in single quotes and escaping any occurrences of the single quote character that appear in it.
This function is useful e.g. when you're templating out shell scripts and can't guarantee that the substituted values only contain "shell-safe" strings.
My question: Is there an equivalent of this written in pure bash with at most coreutils as a dependency? Or perhaps even a bash builtin mechanism I'm not aware of?
Minimal example to show how you would use such a utility (called shlex_quote here as a placeholder):
generate_greeting_script.sh:
#!/bin/bash
cat > greet.sh <<HEREDOC
#!/bin/bash
greeting=$(shlex_quote "$1")
echo "\$greeting \$(whoami)!"
HEREDOC
chmod x greet.sh
$ ./generate_greeting_script.sh "'ello"
$ ./greet.sh
'ello govnah!
Which works just fine with a shlex_quote utility using Python's shlex.quote, but having Python as a dependency just for that is overkill.
CodePudding user response:
Since you refer to bash, you can use the bash specific format character "q" in printf:
$ printf '%q\n' "The world is fuiled by \$s isn't it?"
The\ world\ is\ fuiled\ by\ \$s\ isn\'t\ it\?
From the documentation on bash:
%q
Causes printf to output the corresponding argument in a format that can be reused as shell input.
https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html
CodePudding user response:
I think you're looking Bash-specific printf %q format specifier:
$ read && printf "%q\n" "$REPLY"
"very*complicated_&&;$stuff--string'""""'"<<!!
\"very\*complicated_\&\&\;\$stuff--string\'\"\"\"\"\'\"\<\<\!\!
$ echo \"very\*complicated_\&\&\;\$stuff--string\'\"\"\"\"\'\"\<\<\!\!
"very*complicated_&&;$stuff--string'""""'"<<!!
CodePudding user response:
Or using @Q variable expansion
cat generate_greeting_script:
#!/usr/bin/env bash
cat >greet <<HEREDOC
#!/usr/bin/env bash
printf ${1@Q}' %s!\n' "\$USER"
HEREDOC
chmod x greet
./generate_greeting_script 'I say hello to'
A .sh extension on executable scripts with a shebang is not required.
Generated greet:
#!/usr/bin/env bash
printf 'I say hello to'' %s!\n' "$USER"
Output:
./greet
I say hello to lea!
Reference man bash:
${parameter@operator}Parameter transformation.
The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter:
QThe expansion is a string that is the value of parameter quoted in a format that can be reused as input.
CodePudding user response:
To put the quoted contents of $1 in the variable greeting:
printf -v greeting '%q' "$1"
That uses the Bash-specific -v option and %q format string for printf.
