Fedora Linux Support Community & Resources Center
  #1  
Old 6th September 2005, 03:12 PM
davej Offline
Registered User
 
Join Date: Jun 2005
Location: Bristol, UK
Posts: 15
Escaping bash history expansion character

Here's a little puzzle. I'm trying to enter the string on the following line...
I Didn't Say Anything!
...as an argument to a command. (It's a menu item for a DVD menu). To illustrate the problem, I'll use the echo command.
Code:
$ echo "I Didn't Say Anything!"
bash: !": event not found
I used quotes so that the string gets treated as a single argument, which doesn't matter for 'echo' but does for a DVD menu. I chose double quotes in order to escape the single quote. However, as the Bash Reference Manual says, "Only `\' and `'' may be used to escape the history expansion character". Hence the exclamation mark is treated as an attempt at history expansion, leading to "event not found". OK, so how about single quotes?
Code:
$ echo 'I Didn't Say Anything'
> oops, quote not closed'
I Didnt Say Anything
oops, quote not closed
$ echo 'I Didn\'t Say Anything'
> oops, quote not closed again'
I Didn\t Say Anything
oops, quote not closed again
I can't use single quotes because then I can't escape the quote, not even with a backslash. (The 'oops' lines above are just to illustrate that Bash gave me an unterminated quote prompt on the following line.)
OK, well the Bash Reference Manual says I can use a backslash to escape the exclamation mark, so here goes.
Code:
$ echo "I Didn't Say Anything\!"
I Didn't Say Anything\!
The backslash successfully escapes the exclamation mark, but it doesn't get swallowed up, instead remaining in the resulting string and hence in my DVD menu. Is this a bash bug? I would have thought that when an escape character is operative, it should not appear in the output.

I worked around the problem as follows:
Code:
$ echo I\ Didn\'t\ Say\ Anything\!
I Didn't Say Anything!
I.e. I dispensed with quotes and instead used escaped spaces to keep the string as a single parameter.

So my questions are:
  • Is this a Bash bug?
  • Is there a more elegant solution? Maybe redefining the history expansion character, but I feel that shouldn't be necessary.
Reply With Quote
  #2  
Old 6th September 2005, 05:01 PM
brahms Offline
Registered User
 
Join Date: Aug 2004
Location: Rio
Posts: 434
You don't need to escape spaces. This works fine:
Code:
echo I didn\'t say anything!
__________________
:cool:
Reply With Quote
  #3  
Old 6th September 2005, 05:21 PM
davej Offline
Registered User
 
Join Date: Jun 2005
Location: Bristol, UK
Posts: 15
Hi Brahms,

Thanks for replying but as I said, "I used quotes so that the string gets treated as a single argument, which doesn't matter for 'echo' but does for a DVD menu." I know I don't need the quotes for 'echo' - I was just using echo as an illustration - but I do need them for the 'makemenu' command that I'm running. Otherwise I would get four menu items, "I", "didn't", "say" and "anything!", which is not what I want.

Dave
Reply With Quote
  #4  
Old 6th September 2005, 06:16 PM
Twey Offline
Registered User
 
Join Date: Mar 2005
Location: England
Posts: 867
You can use a combination of quotes.
"I didn't say anything"'!'
__________________
Twey
Reply With Quote
  #5  
Old 6th September 2005, 08:32 PM
davej Offline
Registered User
 
Join Date: Jun 2005
Location: Bristol, UK
Posts: 15
Thanks Twey, that works, I didn't realise you could do that. A better example than 'echo' would have been this:
Code:
$ perl -e 'print map("$_\n",@ARGV)' "I didn't say anything"'!'
I didn't say anything!
Which shows that your solution produces a single string, as required.

I'd still be interested to hear informed opinions about whether Bash is behaving correctly in the example I gave:
Code:
$ echo "I Didn't Say Anything\!"
I Didn't Say Anything\!
It seems to me that the backslash is acting as an escape character here (if you omit it, the exclamation mark is treated as the history expansion character) and so the backslash should not appear in the resulting string.
Reply With Quote
Reply

Tags
bash, character, escaping, expansion, history

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Page Up/Down keys through bash history in xterm; FC11 DZ* Using Fedora 0 24th June 2009 12:10 AM
BASH script gets stuck to prompt after history search sam09 Using Fedora 2 29th February 2008 12:05 PM
Missing my favourite terminal feature! history completion|expansion|substitution? halfshark Using Fedora 9 7th May 2007 05:49 AM
How to disable Bash History? Repo Security and Privacy 1 31st July 2005 08:23 PM


Current GMT-time: 10:25 (Tuesday, 21-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Cabadbaran Instagram Photos - Linqu Travel Photos on Instagram - Suizhou Travel Photos on Instagram