I am trying to do a bash script to check if input file name is of format name.json. The regx in bash doesn't work but it works with grep.
file t1.sh
echo "$1"
regx="^[\w\d-]+[.]json$"
echo $regx
[[ $1 =~ $regx ]] && echo "match" || echo "No Match"
echo $1 | grep -P $regx
test=$(echo $1 | grep -P $regx)
[[ -z $test ]] && echo "Grep No match" || echo "Grep match"
Output
$ ./t1.sh test-file.json
test-file.json
^[\w\d-]+[.]json$
No Match
test-file.json
Grep match
$ ./t1.sh test-file123.json
test-file123.json
^[\w\d-]+[.]json$
No Match
test-file123.json
Grep match
$ ./t1.sh test-file$.json
test-file$.json
^[\w\d-]+[.]json$
No Match
Grep No match
Why is bash not able to process any meta-characters? I thought quoting the regx solved the issue of escape characters.
https://shellcheck.net
, a syntax checker, or installshellcheck
locally. Make usingshellcheck
part of your development process. – waltinator Nov 03 '23 at 02:21