How can I echo an escaped string that contains $ in Bourne Shell?user@server:~$ cat test.sh #!/bin/shecho $1user@server:~$ ./test.sh \$sad\$test$sad$testI want it to return an escaped version like this:user@server:~$ ./test.sh \$sad\$test\$sad\$testI have tried doing tricks with sed and awk but no luck. Any guidance would be greatly appreciated.
How to echo an escaped string
shell;sed;awk
In the script:echo ${1//$/\\$}This is a simple string replacement build into the shell. The syntax is ${[VARIABLE_NAME]//[PATTERN]/[REPLACEMENT]} for a global literal string replacement.Not sure why you would want to do this. The script is actually just reporting the string you sent to it; \$sad\$test evaluates to exactly the same string as '$sad$test', except the latter one uses single quotes to escape the $.