May i know why "~
" and "+$
" is used in this shell script.This is a part of script deleting files older than specific days.
if ! [[ $FILE_AGE =~ "^[0-9]+$" ]]; then
usage
exit 1
I am able to understand that this portion of the script is used to check whether we are entering numbers or alphabets.
The =~
operator means "true if string on left matches the regular expression on the right, else false".
^[0-9]+$
means look for a sequence of one or more (That's the +
) characters in the range 0 to 9 (that's [0-9]
) that start the beginning of the string (^
) and end at the end of the string ($
).
This is basic bash and regex, consult the Bash manpage for more details.
=~ is a regular expression match
$ is part of the regular expression ^[0-9]+$
and it means to match the end of the String
Google "regular expressions" for a tutorial or guide
The =~
is the regex match operator and the +
belongs to the pattern [0-9]+
, not the $
.
The condition tests if the value of the variable FILE_AGE matches the pattern [0-9]+
(one or more digits) between ^
(start of line/string) and $
(end of line/string).
Google for "Bash script equals tilde operator":
As for the dollar sign(s), the first one is because $FILE_AGE
is a variable in Bash, and the second one is because it's part of the regular expression. Here's a great site on RegEx: