Reputation: 107
I'm learning about Linux command line and have come across a regular expression that has me stumped.
Can someone run through this and explain what its doing please. I know this first part as read will use REPLY as a variable if none is supplied.
read -p "Enter a single item > "
# is input a valid filename?
if [[ $REPLY =~ ^[-[:alnum:]\._]+$ ]]; then
echo "'$REPLY' is a valid file name."
Like why is [- in there? Is it checking to see if the start of $REPLY is a dash? Any help would be appreciated.
Upvotes: 0
Views: 86
Reputation: 191789
Expression is:
^ -- Start of the string
[ -- Character class start
- -- A literal dash
[:alnum:] -- POSIX-style alphanumeric (a-z, 0-9)
. -- A literal period
_ -- A literal underscore
] -- End character class
+ -- One or more of anything in the character class
$ -- End of the string
The -
is a literal dash. It is at the start of the character class so that it is not interpreted as a class range. For example, [a-c]
is a, b, c
, but [-ac]
is -, a, c
As an aside, this is an incomplete check for a filename. As far as I know, filenames can contain any character except directory separators (e.g. /
) -- even spaces and newlines in any amount.
Upvotes: 3