LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-14-2008, 10:58 AM   #1
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Rep: Reputation: 30
using special symbols in bash script


Hello, i was wondering how to handle special symbols with bash scripting.

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

they give me error if i use them as variable.

any idea
 
Old 11-14-2008, 11:10 AM   #2
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
You'll usually have to escape them with the \ character.
 
Old 11-14-2008, 11:14 AM   #3
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
you mean to add slash \ after each symbol ?

for example if i want to use them in variable:

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


it wont work!

Last edited by Dr_Death_UAE; 11-14-2008 at 11:19 AM.
 
Old 11-14-2008, 11:19 AM   #4
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by Dr_Death_UAE View Post
you mean to add slash \ after each symbol ?
Yes.

So you want to display the $ symbol, you'd put \$ in you're script, etc.
 
Old 11-14-2008, 11:21 AM   #5
AuroraCA
Member
 
Registered: Jul 2008
Location: Northern CA USA
Distribution: Ubuntu, Slackware, Gentoo, Fedora, Red Hat, Puppy Linux
Posts: 370

Rep: Reputation: 35
The "\" goes before the special character, not after.
 
Old 11-14-2008, 11:23 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Dr_Death_UAE View Post
symbols="!\ #\ $\ %\ &\ '\ (\ )\ *\ +\ ,\ -\ .\ /\ :\ ;\ &\ <\ =\ >\ ?\ @\ [\ \\ ]\ ^\ _\ {\ |\ }\ ~\"
You have to put the escape character before, not after.
Code:
$ echo \! \# \$ \% \& \' \( \) \* \+ \, \- \. \/ \: \; \< \= \< \? \@ \[ \\ \] \^ \_ \{ \| \} \~
! # $ % & ' ( ) * + , - . / : ; < = < ? @ [ \ ] ^ _ { | } ~
 
Old 11-14-2008, 11:28 AM   #7
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
symbols="\! \# \$ \% \& \' \( \) \* \+ \, \- \. \/ \: \; \& \< \= \> \? \@ \[ \\ \] \^ \_ \{ \| \} \~"

gave me syntax error: unexpected end of file
 
Old 11-14-2008, 11:31 AM   #8
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by Dr_Death_UAE View Post
symbols="\! \# \$ \% \& \' \( \) \* \+ \, \- \. \/ \: \; \& \< \= \> \? \@ \[ \\ \] \^ \_ \{ \| \} \~"

gave me syntax error: unexpected end of file
Well that tells us nothing. What are you trying to use the symbols variable as? Can you share the rest of you're script? More details give us better ideas as to what the actual problem is.
 
Old 11-14-2008, 11:45 AM   #9
AuroraCA
Member
 
Registered: Jul 2008
Location: Northern CA USA
Distribution: Ubuntu, Slackware, Gentoo, Fedora, Red Hat, Puppy Linux
Posts: 370

Rep: Reputation: 35
You are defining the variable symbol as a string by using the "" (double quotes). You do not need to use the escape character before characters used as strings. The exception to this rule are the special characters $ (dollar sign) ` (backtick) and \ (backslash).

To do what you are doing you need to use:

Code:
     symbols="! # \$ % & \' ( ) * + , - . / : ; & < = > ? @ [ \\ ] ^ _ { | } ~"
 
Old 11-14-2008, 12:11 PM   #10
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
thanks guys, that solve the problem.

* is also need slash before it. \*
 
Old 11-14-2008, 12:48 PM   #11
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
You could also do:

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 11:56 AM
bash script to find out more than 1 continuous special characters in a file. kkpal Linux - Newbie 1 06-02-2008 04:56 AM
inserting special characters into mysql with bash script ihopeto Linux - Newbie 1 12-05-2006 12:46 PM
BASH: cannot enter special-named dir from script Roger Krowiak Programming 3 08-24-2006 06:19 AM
bash and filenames with special characters CoolAJ86 Programming 2 03-09-2005 02:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration