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


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-10-2017
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:

Code:
$ 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 06:20 AM.. Reason: RudiC Added CODE tags; rbatte1 converted textual list to number formatted list and added ICODE tags for target characters
Sponsored Links
    #2  
Old Unix and Linux 01-10-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 23 July 2017, 6:19 PM EDT
Location: San Jose, CA, USA
Posts: 10,412
Thanks: 527
Thanked 3,638 Times in 3,104 Posts
Does:

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

producing the output:

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

do what you wanted?
Sponsored Links
    #3  
Old Unix and Linux 01-10-2017
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 23 July 2017, 7:50 PM EDT
Posts: 3,414
Thanks: 144
Thanked 1,209 Times in 1,109 Posts
You could also replace anything that isn't acceptable (alphanum and space) like this:


Code:
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
Closed

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 01:42 PM
Escape special characters in SED aakishore Shell Programming and Scripting 2 06-19-2012 04:43 PM
Need help with sed to escape special characters majose Shell Programming and Scripting 4 02-06-2012 04:56 AM
Replace special characters with Escape characters? laknar Shell Programming and Scripting 8 01-05-2012 11:40 PM
awk print $1 escape all special characters unclecameron Shell Programming and Scripting 6 05-29-2010 03:38 PM



All times are GMT -4. The time now is 04:49 PM.