Solved

"!" interpretation in bash command line

Posted on 2011-03-03
5
634 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
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Changing Audit Policies through scripting 5 66
Perl Write to Specific line in a file 15 94
linux pull logs and export FTP 4 71
AWS CLI - Instances, Volumes & Tagging 2 95
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…

809 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