Solved

"!" interpretation in bash command line

Posted on 2011-03-03
5
629 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
5 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 400 total points
Comment Utility
! 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
Comment Utility
... Look at "man bash" under "History Expansion"
0
 
LVL 30

Assisted Solution

by:Kerem ERSOY
Kerem ERSOY earned 50 total points
Comment Utility
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 50 total points
Comment Utility
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 11

Author Closing Comment

by:tel2
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now