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


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
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; 1 Week Ago 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
    #2  
Old Unix and Linux 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 January 2017, 3:12 PM EST
Location: San Jose, CA, USA
Posts: 9,862
Thanks: 476
Thanked 3,385 Times in 2,891 Posts
Does:

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

producing the output:

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

do what you wanted?
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is online now Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 22 January 2017, 3:13 PM EST
Posts: 3,333
Thanks: 135
Thanked 1,137 Times in 1,058 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 (1 Week Ago)
Sponsored Links
Reply

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 04:14 PM.