?
Solved

"!" interpretation in bash command line

Posted on 2011-03-03
5
Medium Priority
?
644 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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