dgsga
dgsga

Reputation: 11

Bash script variable syntax

I am new to bash scripting. I am trying to write a script which searches for a four letter/number string which has the form 'Jxxx' where x can be A-Z or 0-9. How do I represent this as a variable in bash? Thanks very much

Upvotes: 0

Views: 49

Answers (1)

l0b0
l0b0

Reputation: 58788

Variables just contain static content in Bash - what you want is a regular expression, aka. "regex", which in your case would be simplest expressed as J[A-Z0-9][A-Z0-9][A-Z0-9]. You can use this with many programs to match text in files:

$ cat > my.txt << EOF
JAA
JAAA
JZ0Z
J00
foo
EOF
$ grep 'J[A-Z0-9][A-Z0-9][A-Z0-9]' my.txt
JAAA
JZ0Z

or filenames:

$ touch JAA JAAA JZ0Z J00 foo
$ find . -name 'J[A-Z0-9][A-Z0-9][A-Z0-9]'
./JZ0Z
./JAAA

Upvotes: 1

Related Questions