0

I am trying to learn regular expressions, and came across some examples online. Trying to put things together, I inputted this into bash

^(([a-j][a-j]?)|(3[a-j][a-j])$

It returns the following error

bash: !!: event not found.

Why do you believe I am getting that? Should I create a to j files? or should I create 1 file with a-j in it? Why is it returning that? Thank you for your help.

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
Smokefox
  • 1
  • 1
  • Please show exactly how you used that. Cut-and-paste if possible. – glenn jackman Apr 01 '22 at 00:59
  • Hi Glen. I was just testing it as I input it above. e.g. I tested ^/([0-9])|([0-9])$ and I get a different error "substitution failed". So I am just trying to understand the core of it. I have read various articles about expressions but need help in understanding. – Smokefox Apr 01 '22 at 01:12
  • 1
    So you're just typing that into a bash prompt? What do you expect to happen? In bash, you can only use a regular expression as part of a `[[ $var =~ regex ]]` conditional command. See [3.2.5.2 Conditional Constructs](https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs) – glenn jackman Apr 01 '22 at 02:43
  • Shell syntax is extremely context-dependent; bash will only treat something as a regular expression if it occurs in a place where bash expects to see a regular expression (i.e. in `[[ $var =~ regex ]]`). Since you used that in a general command context, bash saw the `^` and interpreted it as a command history substitution (i.e. it thinks you're telling it to recall and re-use some part of a previous command), because that's what `^` means in a general command context. But it couldn't find a matching previous command ("event"), so you get a "event not found" error. – Gordon Davisson Apr 01 '22 at 07:46
  • @glennjackman thank you that is the answer I needed. – Smokefox Apr 01 '22 at 15:41
  • @GordonDavisson Thank you for the breakdown of the matter. I understand it better now. – Smokefox Apr 01 '22 at 15:41
  • You can use https://regexr.com to play with regular expressions. – Tero Kilkanen Apr 02 '22 at 15:23

0 Answers0