LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Fedora (https://www.linuxquestions.org/questions/fedora-35/)
-   -   escaping bash shell (https://www.linuxquestions.org/questions/fedora-35/escaping-bash-shell-624509/)

hectorDUQUE 02-28-2008 10:56 AM

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,

hector

<code>

[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>Vinod1@yahoo.com </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>Vinod1@yahoo.com </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>Vinod1@yahoo.com </Emp_E-mail> </Employee>

SEE HERE, THE MESSAGE HAS THE SCAPE CHARACTER: <\!DOCTYPE

[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]$


</code>

druuna 02-28-2008 11:26 AM

Hi,

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

Code:

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

$ echo $msg
<?xml version="1.0"?> <!DOCTYPE Employee SYSTEM "/home/Employee.dtd"> <Emp_E-mail>Vinod1@yahoo.com </Emp_E-mail> </Employee>

If the double quotes are needed you could also try using the eval command:
Code:

$ fooBar="\!whatever"
$
$ echo $fooBar
\!whatever
$
$ eval echo $fooBar
!whatever
$

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.

chrism01 02-28-2008 07:01 PM

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

hectorDUQUE 02-29-2008 09:59 AM

thanks guys ... it works with single quotes ... any way, i dont like this bash's behaviour :-(

simplicissimus 03-15-2008 05:50 AM

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
aha=1aha

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

... 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.

Confused,
SIMP

Linux Archive

jschiwal 03-15-2008 06:04 AM

The exclamation point is used for history expansion in an interactive shell.

Quote:

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

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>Vinod1@yahoo.com </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.


All times are GMT -5. The time now is 09:07 AM.