We can use the following BASH function escape to escape the parameter strings and put them one by one. For example:
1 2 3 4 5 6 7 | #!/bin/bash function escape () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done } escape $* |
#!/bin/bash function escape () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done } escape $*
Using the prinftf with sed to escape the strings:
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | $ ./escape 1234 a 4 "5 a" '1234' \ 'a' \ '4' \ '5' \ 'a' \ $ ./escape 1234 a 4 '' '1234' \ 'a' \ '4' \ $ ./escape 1234 a 4 ' ' '1234' \ 'a' \ '4' \ $ ./escape 1234 a 4 'ss1234' '1234' \ 'a' \ '4' \ 'ss1234' \ $ ./escape 1234 a 4 'ss1234' "'" '1234' \ 'a' \ '4' \ 'ss1234' \ ''\''' \ $ ./escape 1234 a 4 'ss1234' "''" '1234' \ 'a' \ '4' \ 'ss1234' \ ''\'''\''' \ $ ./escape 1234 a 4 'ss1234' "<>" '1234' \ 'a' \ '4' \ 'ss1234' \ '<>' \ $ ./escape 1234 a 4 'ss1234' "<>" '1234' \ 'a' \ '4' \ 'ss1234' \ '<>' \ |
$ ./escape 1234 a 4 "5 a" '1234' \ 'a' \ '4' \ '5' \ 'a' \ $ ./escape 1234 a 4 '' '1234' \ 'a' \ '4' \ $ ./escape 1234 a 4 ' ' '1234' \ 'a' \ '4' \ $ ./escape 1234 a 4 'ss1234' '1234' \ 'a' \ '4' \ 'ss1234' \ $ ./escape 1234 a 4 'ss1234' "'" '1234' \ 'a' \ '4' \ 'ss1234' \ ''\''' \ $ ./escape 1234 a 4 'ss1234' "''" '1234' \ 'a' \ '4' \ 'ss1234' \ ''\'''\''' \ $ ./escape 1234 a 4 'ss1234' "<>" '1234' \ 'a' \ '4' \ 'ss1234' \ '<>' \ $ ./escape 1234 a 4 'ss1234' "<>" '1234' \ 'a' \ '4' \ 'ss1234' \ '<>' \
BASH Programming/Shell
- Three Interesting/Fun BASH Commands
- One Interesting Linux Command (Steam Locomotive in BASH)
- Simple Bash Function to Repeat a Command N times with Retries
- How to Extract a Domain Name from a Full URL in BASH?
- BASH Function to Compute the Greatest Common Divisor and Least Common Multiples
- BASH Function to Get Memory Information via AWK
- BASH Function to Escape Parameters Argument
- BASH Function to Check if sudo Available
- Full Permutation Algorithm Implementation in BASH
- BASH Function to Install Docker
- A Simple BASH Function/Script to Run a Command in Background
- BASH Script to Compute the Average Ping to a Domain
- A Bash Process Controller to Start, Stop and Restart an Daemon Program
- Linux BASH shell - Echo This if you get angry
- Bash SHELL, Chess Board Printing
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
236 wordsloading...
Last Post: Teaching Kids Programming - Count Square Sum (Pythagorean) Triples
Next Post: Teaching Kids Programming - Brick Layout (Unlimited Knapsack) via Top Down Dynamic Programming Algorithm