Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Fedora This forum is for the discussion of the Fedora Project.


  Search this Thread
Old 02-28-2008, 10:56 AM   #1
Registered: Jan 2007
Posts: 121

Rep: Reputation: 15
escaping bash shell

hi guys,
i am trying to scape a "!" character in bash shell, but it looks having a bug in bash shell ...

the problem is a "!DOCTYPE" xml statement. When i try to asign a XML message to a variable, bash shell rejects the special character, when i scape the character (using \), bash shell accept it, BUT DO NOT clear that character from the message, which comes in a XML error.

i have fedora c6 an bas shell:
GNU bash, version 3.1.17(1)-release (i686-redhat-linux-gnu)

am i doing something wrong ?

thanks in advance,



[duque32@unknown-00-13-8f-c4-40-21 engine]$ msg="<?xml version=\"1.0\"?> <!DOCTYPE Employee SYSTEM \"/home/duque32/rouletteProjects/rouletteLibrariesInstallDir/kg_XMLLib/DTDs/Employee.dtd\"> <Employee> <Emp_Id>E-001 </Emp_Id> <Emp_Name>Vinod </Emp_Name> <Emp_E-mail> </Emp_E-mail> </Employee>"
bash: !DOCTYPE: event not found
[duque32@unknown-00-13-8f-c4-40-21 engine]$
[duque32@unknown-00-13-8f-c4-40-21 engine]$
[duque32@unknown-00-13-8f-c4-40-21 engine]$
[duque32@unknown-00-13-8f-c4-40-21 engine]$ msg="<?xml version=\"1.0\"?> <\!DOCTYPE Employee SYSTEM \"/home/duque32/rouletteProjects/rouletteLibrariesInstallDir/kg_XMLLib/DTDs/Employee.dtd\"> <Employee> <Emp_Id>E-001 </Emp_Id> <Emp_Name>Vinod </Emp_Name> <Emp_E-mail> </Emp_E-mail> </Employee>"
[duque32@unknown-00-13-8f-c4-40-21 engine]$
[duque32@unknown-00-13-8f-c4-40-21 engine]$ echo $msg
<?xml version="1.0"?> <\!DOCTYPE Employee SYSTEM "/home/duque32/rouletteProjects/rouletteLibrariesInstallDir/kg_XMLLib/DTDs/Employee.dtd"> <Employee> <Emp_Id>E-001 </Emp_Id> <Emp_Name>Vinod </Emp_Name> <Emp_E-mail> </Emp_E-mail> </Employee>


[duque32@unknown-00-13-8f-c4-40-21 engine]$
[duque32@unknown-00-13-8f-c4-40-21 engine]$
[duque32@unknown-00-13-8f-c4-40-21 engine]$ bash -version
GNU bash, version 3.1.17(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
[duque32@unknown-00-13-8f-c4-40-21 engine]$

Old 02-28-2008, 11:26 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389

Do you need to use double quotes? It seems that single quotes solve your problem:

$ msg='<?xml version="1.0"?> <!DOCTYPE Employee SYSTEM "/home/Employee.dtd"> <Emp_E-mail> </Emp_E-mail> </Employee>'

$ echo $msg
<?xml version="1.0"?> <!DOCTYPE Employee SYSTEM "/home/Employee.dtd"> <Emp_E-mail> </Emp_E-mail> </Employee>
If the double quotes are needed you could also try using the eval command:
$ fooBar="\!whatever"
$ echo $fooBar
$ eval echo $fooBar
It doesn't explains bash's behaviour. I do recall that I also encountered this on previous bash versions and don't think it is a bug.

Anyway, hope this gets you going again.
Old 02-28-2008, 07:01 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
I'm not quite clear what you want : do you want to

1. escape it - which you seem to have done ok
2. ignore it - use single quotes as suggested, although you'd have to be careful using that var
3. remove it (!) - use sed
Old 02-29-2008, 09:59 AM   #4
Registered: Jan 2007
Posts: 121

Original Poster
Rep: Reputation: 15
thanks guys ... it works with single quotes ... any way, i dont like this bash's behaviour :-(
Old 03-15-2008, 05:50 AM   #5
Registered User
Registered: Mar 2008
Posts: 104

Rep: Reputation: 15
Runnings these tests ...

$ my=! && echo $my
$ my='!' && echo $my
$ my="!" && echo $my
bash: !: event not found

... it looks like Bash is using '!' as a protected operator within double quotes only. Running additional tests in exactly this order ...

$ aha=1

$ my="!$aha" && echo $my
my="aha=1aha" && echo $my

$ my="!aha" && echo $my
my="aha=1" && echo $my

... it seems that '!' makes echoing the entire line where 'aha' was set. The user's .bash_history contains the echoed line, not the one with '!'.

There is some weird concept behind this (madness has a method), but I don't see any practical use for this right now.


Linux Archive

Last edited by simplicissimus; 04-02-2008 at 04:32 AM.
Old 03-15-2008, 06:04 AM   #6
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
The exclamation point is used for history expansion in an interactive shell.

Originally Posted by bash info manual
History expansions are introduced by the
appearance of the history expansion character, which is `!' by default.
Only `\' and `'' may be used to escape the history expansion character.
So you need to use single quotes in the assignment statement:
 msg='<?xml version="1.0"?> <!DOCTYPE Employee SYSTEM "/home/duque32/rouletteProjects/rouletteLibrariesInstallDir/kg_XMLLib/DTDs/Employee.dtd"> <Employee> <Emp_Id>E-001 </Emp_Id> <Emp_Name>Vinod </Emp_Name> <Emp_E-mail> </Emp_E-mail> </Employee>'
Otherwise, bash is looking for a line in the history buffer starting with "DOCTYPE" to run.

You would want to use single quotes anyway because your string contains doublequotes. That way you don't have to escape them.

Last edited by jschiwal; 03-15-2008 at 06:06 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic special character escaping in Bash scripts? wipe Linux - Software 1 06-05-2009 06:41 PM
Run a C Shell using only Bash shell HSN Linux - Software 1 12-25-2006 07:44 AM
escaping ':' character in bash script paulyche Linux - General 2 11-07-2006 07:18 AM
Bash script - escaping whitespaces colabus Linux - Newbie 15 04-29-2006 11:58 PM
NOT escaping bash history ivanatora Linux - General 3 06-07-2005 01:17 PM > Forums > Linux Forums > Linux - Distributions > Fedora

All times are GMT -5. The time now is 11:37 PM.

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