0

I am trying to pass a regex pattern as an argument to my script as follows:
bash script.sh '[a-z]*[0-9]*'

in my script I am assigning it to a variable
FIRST_ARG=$1
using echo $FIRST_ARG results in showing the files that match the expression, (i.e text1.txt, file2.txt.

I want the $1 to stay [a-z]*[0-9]*, I tried wrapping every variable with double quotes and without quotes and that didn't work, I also tried using single quotes, double quotes and no quotes on the command line argument and it doesn't work either.

Any ideas on how to fix this? Thank you.

terdon
  • 242,166
Yoh
  • 1
  • When you run echo $1, the shell will expand the glob (your regex pattern is treated as a glob in this context), and that's why you see the matching files. You need to always quote your variables, so use echo "$FIRST_ARG" or, even better, printf '%s\n' -- "$FIRST_ARG", and it will work as expected. – terdon Jul 30 '22 at 12:46
  • I realized what I've done after posting the question, I've been looking online for hours trying to get an answer lol, not my best day. – Yoh Jul 30 '22 at 12:53
  • No worries, this sort of thing confuses all of us until we get used to it! Read the answers to the duplicate, they have some really good info. – terdon Jul 30 '22 at 12:55

0 Answers0