I ran into this stuff when using sed to filter rsync stdout.
One of the responders in the link above built a table for himself, That may be a good idea; Build a table with examples that work for you. Building one for myself was a learning experience.
For simple things where you need to include a variable, double quotes works.
However…bash quoting can get really complex really fast if your text or variables may contain quotes or other special characters in them (e.g. things like filenames or passwords).
#!/bin/bash
# below function takes incoming text and
# 1) replaces all ' with '"'"', then
# 2) adds ' to the beginning and
# 3) adds ' to the end
# This creates a quoted string that can be passed by bash to other programs,
# avoiding special character expansion (regardless of original content).
esc() {
printf "%s" "$1" | sed -e "s/'/'\"'\"'/g" -e "1s/^/'/" -e "\$s/\$/'/"
}
Example usage:
# using escaping to create this variable value, sans brackets [test 1/23 '" ]
value='test 1/23 '"'"'" '
echo ${value}
escaped_value_with_single_quotes=$(esc "${value}")
# the value below may be safer to use later in the script if you are constructing
# a commandline to another program.
echo ${escaped_value_with_single_quotes}