Usage of dollar and tilde

Go To StackoverFlow.com

1

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.

2012-04-04 17:51
by user1313487
You must not quote the right-hand side if you're using bash version 4. If it's quoted, bash will interpret it as a literal string. It's not super clearly documented, but the man page says: "Any part of the pattern may be quoted to force it to be matched as a string - glenn jackman 2012-04-04 18:31


6

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.

2012-04-04 17:58
by cha0site


1

=~ 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

2012-04-04 17:58
by Jon Lin


1

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).

2012-04-04 18:03
by Mithrandir


0

Google for "Bash script equals tilde operator":

http://unixdog.com/?p=94

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:

http://www.regular-expressions.info/reference.html

2012-04-04 17:57
by qJake
Ads