[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

"!" interpretation in bash command line

Posted on 2011-03-03
5
Medium Priority
?
648 Views
Last Modified: 2012-05-11
Hi Experts,

In regard to commands entered at UNIX/Linux shell command lines (I'm not talking about scripts), if I do this in ksh, it works fine:
    echo "Hi!"
but in bash, the same command gives me this error:
    !": event not found
I realise I can work around that by escaping the "!", like this:
    echo "Hi\!"
but I still have some questions about the un-escaped command in bash:

1. Why do I get that error?  Obviously "!" means something special in the bash command line, but what?  I had a quick Google, to no avail, but I'd rather not take all day, and finding this in "man bash" could take ages too.

2. Why can I not up-arrow to go back through my command history, to that uncommand, and change it?  It works for general commands containing syntax errors.

3. Is there a way to disable this behaviour in for the bash command line (i.e. so I don't have to escape the "!")?

Thanks.
0
Comment
Question by:tel2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1600 total points
ID: 35032029
! Is used to recall the previous command issued.
You can't recall it because the shell won't add it to the command history.
A space following it should do, as well as single quotes.
Turn it off by setting
histchars=

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35032048
... Look at "man bash" under "History Expansion"
0
 
LVL 30

Assisted Solution

by:Kerem ERSOY
Kerem ERSOY earned 200 total points
ID: 35032065
Double quotes won't stop the evaluation of special caharacters. In this case ! recalls the history.
Please use single quotes around the string such as:

echo 'Hi!'

This will stop the execution of special characters. The enclosed string will be treated as ordinary text without expansion/execution.

Cheers,
K.
0
 
LVL 9

Assisted Solution

by:AriMc
AriMc earned 200 total points
ID: 35032084
1. The exclamation mark used to have a special meaning of aliasing to "history" or the previous commands you entered on the command line. It's still present as such on some shells.

2. The up-arrow is the same as going back in the command history so those commands with exclamation marks are not saved in the history.

3. You could try single quotes, ie.    echo 'Hi!'
0
 
LVL 12

Author Closing Comment

by:tel2
ID: 35032226
Wow you guys are fast!

Thanks for your prompt and excellent answers.

Most points will go to wmp, because he covered almost everything, and got in first.  I tried 'histchars=' and it works, so it is now in my .bashrc file.

Keep up the good work, guys!

BTW wmp, I did a search for "History Expansion" in "man bash", but it found nothing.  I'm using GNU Bash 3.0.  Wait - just found it in caps - "HISTORY EXPANSION".  As you were.  Now, where's the backspace key on this keyboard?

Later.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question