Solved

"!" interpretation in bash command line

Posted on 2011-03-03
5
637 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 400 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 50 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 50 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell to pull all Service accounts 1 78
aix unix tar error 3 82
awk file 6 102
WMIC Output Formatting 1 49
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

726 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