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.
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 useecho "$FIRST_ARG"
or, even better,printf '%s\n' -- "$FIRST_ARG"
, and it will work as expected. – terdon Jul 30 '22 at 12:46