Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to escape all special characters?

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 01-10-2017   -   Original Discussion by AMG1978
AMG1978's Unix or Linux Image
AMG1978 AMG1978 is offline
Registered User
Join Date: Jan 2017
Last Activity: 11 January 2017, 11:27 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How to escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters
  1. '
  2. [
  3. ]

My attempt is as follows:

$ echo '`~!@#$%^&*()-_=+{}\|;:",<.>/?' | sed 's/[`~!@#$%^&*()-_=+{}\|;:",<.>/?]/\\&/g'

If I add [ or ] it obviously breaks my sed logic.
If I add ' it obviously causes errors in sed as it ruins the sed format and the unexpected ' later (which should remain)

If this is not possible, I will tell the client, all characters are acceptable, but generate passwords without ' [ or ].
I wonder.... maybe I escape EVERY SINGLE Character? I'd have to test the app... and still not sure how I can do that with sed properly? Is that the solution... not sure.
Please let me know if you can help.
Thank you!

Moderator's Comments:
How to escape all special characters? Please use CODE tags as required by forum rules!

Last edited by rbatte1; 01-11-2017 at 07:20 AM.. Reason: RudiC Added CODE tags; rbatte1 converted textual list to number formatted list and added ICODE tags for target characters
Sponsored Links
Old Unix and Linux 01-10-2017   -   Original Discussion by AMG1978
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 21 January 2018, 11:05 PM EST
Location: San Jose, CA, USA
Posts: 10,929
Thanks: 611
Thanked 3,819 Times in 3,263 Posts

echo "'"'[]`~!@#$%^&*()-_=+{}\|;:",<.>/?' | sed 's/[][`~!@#$%^&*()-_=+{}\|;:",<.>/?'"'"']/\\&/g'

producing the output:


do what you wanted?
Sponsored Links
Old Unix and Linux 01-10-2017   -   Original Discussion by AMG1978
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Join Date: Oct 2010
Last Activity: 18 January 2018, 9:44 PM EST
Posts: 3,434
Thanks: 149
Thanked 1,216 Times in 1,115 Posts
You could also replace anything that isn't acceptable (alphanum and space) like this:

sed 's/[^a-zA-Z 0-9]/\\&/g'

The Following User Says Thank You to Chubler_XL For This Useful Post:
rbatte1 (01-11-2017)
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to escape Special Characters in Expect programming? Mahesh Desai Shell Programming and Scripting 1 11-28-2012 02:42 PM
Escape special characters in SED aakishore Shell Programming and Scripting 2 06-19-2012 05:43 PM
Need help with sed to escape special characters majose Shell Programming and Scripting 4 02-06-2012 05:56 AM
Replace special characters with Escape characters? laknar Shell Programming and Scripting 8 01-06-2012 12:40 AM
awk print $1 escape all special characters unclecameron Shell Programming and Scripting 6 05-29-2010 04:38 PM

All times are GMT -4. The time now is 02:04 AM.