[BASH] Allow name with spaces (regex)

Hey all,

I have a very simple regular expression that I use when I want to allow only letters with spaces. (I know this regex has a lot of shortcomings, but I'm still trying to learn them)

isAlpha='^[a-zA-Z\s]*$'

However, when I bring this over to BASH it doesn't allow me to enter spaces.

I use the following code to produce a variable, which I then check if check is empty or not:

check=`echo $name | sed "s/\($isAlpha\)//"`

Any suggestions are greatly appreciated.

---------- Post updated at 07:52 PM ---------- Previous update was at 07:32 PM ----------

Sorry Scott,

When I say enter spaces I mean if I entered the following name:

John James Doe

That should be a valid match, but it isn't. Instead I have to enter

JohnJamesDoe (no spaces)

For it to be valid. I would like to be able to enter a name with a space (as in example 1)

In regards to the "remembered expression", I was following some online tutorials and that's how it showed me. If it is incorrect, I would appreciate it if you could show me the right way.

[Edit]: I did hit "post reply" but it just edited my main post and appeared to delete Scott's post... Sorry for the confusion.

Yes, I got it! Too much Vino over Christmas, I think :smiley:

Anyway, this \s stuff doesn't work for me.

Perhaps change the "\s" to " \t" (space \t).

Your sed could also change, to:

sed "s/$isAlpha//g"

Or you could just use "grep -v $isAlpha" instead of sed.

edit: This is fun :slight_smile: I deleted my post, no need to worry there

1 Like

Haha, I wondered what I had done!

Anyway, that " /t" works a treat. Just what I needed.

Thank you very much!:o

You can check this with bash itself, without invoking an external command at all. An example:

var='John Doe'
[[ $var =~ ^[[:alpha:][:space:]]*$ ]] && echo ok

without using bash's [[ you have options like so:

case "$var" in *[^A-Za-z\ ]*) echo illegal;; *) echo ok;; esac