Exclamation point in Bash | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Exclamation point in Bash

Shell Programming and Scripting


Tags
bash exclamation point

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-19-2009
Enobarbus37 Enobarbus37 is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 25 July 2010, 10:25 AM EDT
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation point in Bash

Here is a script using egrep that extracts the last word in each line:

egrep -o " [a-zA-Z'-]+[,:.!';?]*$" File.txt > Results.txt

If it is placed in a file with the requisite header:

#!/bin/bash

it works perfectly.

If on the other hand, one attempts it from the command line, one gets the following error:

-bash: !': event not found

The problem is the exclamation point in the regexp.

I have tried numerous ways to get it to work from the command line and failed: using single quotes, escaping the !, etc.

Nothing works.

Now, you may ask, if it works from a file, why insist on doing it from the command line? You're right. I shouldn't. And my research indicates that this problem is particularly annoying in bash and that people simply recommend using a different shell.

But can it be solved?

Thanks in advance...
Sponsored Links
    #2  
Old 05-19-2009
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 20 January 2014, 5:51 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
Escape the !
On my pC whith Cygwin :

Code:
$ egrep -o " [a-zA-Z'-]+[,:.!';?]*$" file.txt > result.txt
bash: !': event not found

$ egrep -o " [a-zA-Z'-]+[,:.\!';?]*$" file.txt > result.txt
$

Jean-Pierre.
Sponsored Links
    #3  
Old 05-19-2009
Enobarbus37 Enobarbus37 is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 25 July 2010, 10:25 AM EDT
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I had only recently started using the double quotes in the expression, and I had not yet gotten around to escaping the ! in the double quotes.

So, you are right. Félicitations et chapeau. Mille mercis.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to make your bash script run on a machine with csh and bash npatwardhan Shell Programming and Scripting 3 11-19-2008 03:17 AM
BASH: floating point exception mapleleaves Shell Programming and Scripting 5 08-22-2008 08:25 AM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 12:25 PM
Floating point numeric comparisions in bash borncrazy Shell Programming and Scripting 2 03-27-2005 06:39 PM
Fibre connection Point to Point SUN kie UNIX for Advanced & Expert Users 6 05-20-2003 11:20 AM



All times are GMT -4. The time now is 03:12 PM.