Use printf
(1):
x='a real \good %* load of c$rap'
x=$(printf '%q' "$x")
echo $x
will return
a\ real\ \\good\ %\*\ load\ of\ c\$rap
More Related Contents:
- How to escape single quotes in Bash/Grep?
- How to escape a single quote in single quote string in Bash?
- Bash script prints “Command Not Found” on empty lines
- What’s the magic of “-” (a dash) in command-line parameters?
- Is there a “goto” statement in bash?
- Expanding a bash array only gives the first element
- How can I generate new variable names on the fly in a shell script?
- How can I preserve quotes in printing a bash script’s arguments
- Bash while read loop extremely slow compared to cat, why?
- Automatically capture output of last command into a variable using Bash?
- Find multiple files and rename them in Linux
- How to find the last field using ‘cut’
- How can I loop over the output of a shell command?
- Difference between ${} and $() in Bash [duplicate]
- [ :Unexpected operator in shell programming [duplicate]
- How to redirect output of an already running process [duplicate]
- Command to get time in milliseconds
- How to create a hex dump of file containing only the hex characters without spaces in bash?
- How to sort strings that contain a common prefix and suffix numerically from Bash?
- How to create a CPU spike with a bash command
- Why can’t I use Unix Nohup with Bash For-loop?
- Prevent Gnome Terminal From Exiting After Execution [duplicate]
- Syntax with pound and percent sign after shell parameter name [duplicate]
- Insert multiple lines into a file after specified pattern using shell script
- Get ceiling integer from number in linux (BASH)
- How can I trim white space from a variable in awk?
- Counter increment in Bash loop not working
- Start script after another one (already running) finishes
- Problem with Bash output redirection [duplicate]
- Why exit code 141 with grep -q?