0

I've been trying to add a user-defined file extension into a regex with no success. Here's my script:

#!/bin/bash

echo -n "Enter which file extension are you looking for: "

read fExt

find . -maxdepth 1 -type f -regextype posix-extended -regex '^./q[0-9]{1,2}[a-z]?.\"$fExt\"$'

The file names I'm looking for look like

q1.txt
q22b.pdf
q3c.JPG

This version doesn't work so I've been using multiple ext1|ext2|... cases, which obviously isn't modular. How would I add $fExt into my -regex input? Is it possible in the first place, or is there an easier way to go about this? Thanks!

estro
  • 37
  • 4
  • Possible duplicate of [Expansion of variable inside single quotes in a command in bash shell script](http://stackoverflow.com/questions/13799789/expansion-of-variable-inside-single-quotes-in-a-command-in-bash-shell-script) – Benjamin W. Feb 07 '16 at 03:57
  • And even if it's not an exact duplicate: that's the problem. – Benjamin W. Feb 07 '16 at 04:01
  • yep that post was exactly what I was looking for, despite looking for it for awhile. cheers @BenjaminW. – estro Feb 07 '16 at 04:12

1 Answers1

2

The way you've used single-quotes prevents $fExt from being expanded.

Assuming a bash-like shell, the regex you seem to want could be specified as follows:

"^q[0-9]{1,2}[a-z]?\.$fExt\$"

or

$'^q[0-9]{1,2}[a-z]?\\.'"$fExt"'$'
peak
  • 105,803
  • 17
  • 152
  • 177