Running Regular Expressions in Case Statements in Bash
This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs.
Introduction to Regular Expressions
A regular expression, also called regex or regexp, is a sequence of characters used for text/string matching. Regular expressions are very powerful and can save a lot of time when you need to parse large amounts of data.
Although Bash does not use regular expressions, it uses string matching, and its syntax is similar to that of regular expressions. The following script will help you become familiar with the basics of string matching using Bash.
?(a pattern list)
# Matches exactly zero or one instance of the pattern
*(a pattern list)
# This matches zero or more instances of the pattern.
+(a pattern list)
# This matches one or more instances of the pattern.
@(a pattern list)
# This matches one of the enclosed patterns.
!(a pattern list)
# This matches any pattern except the one enclosed.
The code column above shows basic regular expression syntax; if you want to learn more about regular expressions.
The need to enable complex string matching in Bash
By default, you can run simple regular expressions in Bash; complex regular expressions will require you to enable the extglob option. The following commands will show you how to enable or disable extglob to allow you to run complex regular expressions.
shopt -s extglob # this command enables extglob and allows the execution of complicated regex
shopt -u extglob # this command disables extglob and disables execution of complicated regex
extglob in the above command stands for extended globbing. If set, it allows complex/advanced pattern matching capabilities given under pathname expansion.
It is important to note that without running a regular expression it is impossible to know if it is complex. Enabling the extglob option will save you from any pain and run all commands without worries.
One question you may have is how do we know whether extglob is on or off. See the following command for reference.
shopt | grep extglob # displays status of extglob
Output:
extglob off # displays this line if extglob is disabled
extglob on # displays this line if extglob is enabled
The above command will display the status of extglob, whether it is on or off.
Using regular expressions for string matching in conditional structures
String matching (similar to regular expressions) can be made more powerful through the case structure, and we want to use it to let ourselves parse more complex data. The following commands will help you complete the use of case in Bash.
case EXPRESSION in
Match_1)
STATEMENTS # run this statement if in matches match_1
;;
Match_2)
STATEMENTS # run this statement if in matches match_2
;;
Match_N)
STATEMENTS # run this statement if in matches match_N
;;
*)
STATEMENTS # otherwise, run this statement
;;
Esac # signals the end of the case statement to the kernel
The above code is the general syntax you will use in case you want to combine Bash string matching with the case construct.
By default, Bash allows simple pattern matching. For example, it will successfully execute the following Bash command.
cat sh*
# the above command displays the contents of all files whose name begins #with sh to the monitor (or stdout)
However, given the following command (using complex pattern matching), you will get an error bash: syntax error near unexpected token '(' .
cat +([0-9]) # this command displays contents of files whose names are
# entirely composed of numbers
If you want to explore string matching in Bash further, use the following command.
man bash # man is a short form of manual pages here
A man page is a utility that can be used to find information about any Bash command, system call, and so on.
If you use string matching and case in Bash, it is a good idea to first declare and define a variable against which you will compare the pattern.
We use string matching in the following command snippet. Notice how we use the basics of string matching to produce a powerful string matching algorithm.
Let's look at the commands.
# Remember to not forget to enable extglob
shopt -s extglob # enables extglob
shopt | grep extglob # checks if extglob is enabled
some_variable="rs-123.host.com"; # declare and define variable
case $some_variable in
ab-+([0-9])\.host\.com) echo "First 2 characters were ab"
;;
ks-+([0-9])\.host\.com) echo "First 2 characters were ks"
;;
cs-+([0-9])\.host\.com) echo "First 2 characters were cs"
;;
*)echo "unknown first 2 characters"
;;
esac;
# the above command will display the unknown first 2 characters as we
# don't have a match in the first three cases, so the last default one will #automatically be true
Output:
unknown first 2 characters
If you analyze the string matching command above, if the first two characters are one of (ab,ks,cs), the next character is a hyphen (-) followed by any number of digits, ending with .host.com, then one of the first three cases will succeed and the appropriate message will be displayed.
However, if this is not the case, then the default (i.e. else case) will run and we will receive a message: Unknown first 2 characters.
If you feel the above commands are too complicated, we have a much simpler solution. The commands below explain exactly how.
some_variable="rs-123.host.com"; # declare and define variable
case $some_variable in
ab*.host.com) echo "First 2 characters were ab"
;;
# the command below stays the same
The command above is identical to the more complex case structure we used above.
Using regular expressions for string matching in If-Else structures
Another way to write powerful string matching algorithms is to use them with if-else structures. We want to use this to parse more complex data.
The following Bash commands will walk you through the syntax of the if-else structure.
if expression1
then
task1
elif expression2
then
task2
else
task3
fi # signals ending of if else structure
Using the if-else structure is easy; first, we evaluate the first expression.
If true, we perform task 1. Otherwise, we consider the second expression.
If true, then execute task 2, otherwise execute task 3.
Now that you are familiar with the syntax of the if-else structure, let's copy the commands used in the case structure to the equivalent if-else structure. See the commands below to do this.
# Remember to not forget to enable extglob
shopt -s extglob # enables extglob
shopt | grep extglob # checks if extglob is enabled
some_variable="rs-123.host.com"; # declare and define variable
if expr "$some_variable" : ‘ab-+([0-9])\.host\.com’ >/dev/null; then echo "First 2 characters were ab"
elif expr "$some_variable" : ‘ks-+([0-9])\.host\.com’ >/dev/null; then echo "First 2 characters were ks"
elif expr "$some_variable" : ‘cs-+([0-9])\.host\.com’ >/dev/null; then echo "First 2 characters were cs"
else echo "unknown first 2 characters"
fi
# the above command will display the unknown first 2 characters as we
# don't have a match in the first three cases, so the last default one will #automatically be true
Output:
unknown first 2 characters
The command above is the if-else equivalent of the case structure we used earlier.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u