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
Solve R command not found on Bash (or Cygwin)
Publish Date:2025/03/21 Views:102 Category:OPERATING SYSTEM
-
Commands may sometimes behave differently than you expect, even if it appears that you have done everything right, such as in the case of bash: '\r': command not found or similar error messages, for example syntax error near unexpected toke
How to fix the error Make Command Not Found in Cygwin
Publish Date:2025/03/21 Views:120 Category:OPERATING SYSTEM
-
Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err
Error handling in Bash
Publish Date:2025/03/21 Views:94 Category:OPERATING SYSTEM
-
This article introduces error handling in bash. Remember, understanding exit codes, options such as errexit and trap allow us to build robust scripts and manage bash problems more effectively. Exit Codes in Bash Handling errors based on exi
Getting the absolute path in Bash
Publish Date:2025/03/21 Views:60 Category:OPERATING SYSTEM
-
In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file. Before we begin, we need to understand the basic concepts of absolute
Difference between Bash Nohup and &
Publish Date:2025/03/21 Views:151 Category:OPERATING SYSTEM
-
This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux Processes in the Backgr
Renaming Files in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way
Open Emacs in Bash
Publish Date:2025/03/21 Views:142 Category:OPERATING SYSTEM
-
This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit
Clear the Terminal Screen in Bash
Publish Date:2025/03/21 Views:145 Category:OPERATING SYSTEM
-
There are various ways to clear the terminal in bash script. This article will discuss 3 methods to clear the terminal. Use tput reset to clear the terminal screen. The first method uses the keyword tput reset to clear the screen. When your
Reload .bash_profile from the command line
Publish Date:2025/03/21 Views:67 Category:OPERATING SYSTEM
-
In the shell, .bash_profile is used to customize the configuration of user settings. It is stored in the root directory or home directory and is mostly hidden from other users. This file holds all the configuration of the shell and is also