Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to escape a string in bash

Tags:

bash

escaping

I need a bash command that will convert a string to something that is escaped. Here's an example:

echo "hello\world" | escape | someprog 

Where the escape command makes "hello\world" into "hello\\\world". Then, someprog can use "hello\\world" as it expects. Of course, this is a simplified example of what I will really be doing.

like image 709
User1 Avatar asked May 18 '10 04:05

User1


1 Answers

In Bash:

printf "%q" "hello\world" | someprog 

for example:

printf "%q" "hello\world" hello\\world 

This could be used through variables too:

printf -v var "%q\n" "hello\world" echo "$var" hello\\world 
like image 52
Dennis Williamson Avatar answered Nov 07 '22 00:11

Dennis Williamson