InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Testing with Pattern Matches in bash
Bash

Testing with Pattern Matches in bash

InfinityCoder February 14, 2017

You want to test a string not for a literal match, but to see if it fits a pattern.

For example, you want to know if a file is named like a JPEG file might be named.

Use the double-bracket compound statement in an if statement to enable shell-style pattern matches on the righthand side of the equals operator:

1
if [[ "${MYFILENAME}" == *.jpg ]]

The double-brackets is a newer syntax (bash version 2.01 or so). It is not the oldfashioned [ of the test command, but a newer bash mechanism.

It uses the same operators that work with the single bracket form, but in the double-bracket syntax the equal sign is a more powerful string comparator.

The equal sign operator can be a single equal sign or a double equals as we have used here.

They are the same semantically. We prefer to use the double equals (especially when using the pattern matching) to emphasize the difference, but it is not the reason that we get pattern matching—that comes from the double-bracket compound statement.
The standard pattern matching includes the * to match any number of characters, the question mark (?) to match a single character, and brackets for including a list of possible characters.

Note that these resemble shell file wildcards, and are not regular expressions.
Don’t put quotes around the pattern if you want it to behave as a pattern.

If our string had been quoted, it would have only matched strings with a literal asterisk as the first character.
There are more powerful pattern matching capabilities available by turning on some additional options in bash.

Let’s expand our example to look for filenames that end in either .jpg or .jpeg. We could do that with this bit of code:

1
2
3
4
shopt -s extglob
if [[ "$FN" == *.@(jpg|jpeg) ]]
then
   # and so on

The shopt -s command is the way to turn on shell options. The extglob is the option dealing with extended pattern matching (or globbing).

With this extended pattern matching we can have several patterns, separated by the | character and grouped by
parentheses.

The first character preceding the parentheses says whether the list should match just one occurrence of a pattern in the list (using a leading @) or some other criteria.

Table 6-4 lists the possibilities (see also “extglob Extended Pattern-Matching Operators” in Appendix A ).

Table 6-4. Grouping symbols for extended pattern-matching

Grouping Meaning
@( … )

*( … )

+( … )

?( … )

!( … )

Only one occurrence

Zero or more occurrences

One or more occurrences

Zero or one occurrences

Not these occurrences, but anything else

Matches are case sensitive, but you may use shopt -s nocasematch (in bash versions 3.1+) to change that.

This option affects case and [[ commands.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Setting Up a Database with MySQL in bash
You want to create and initialize several databases using MySQL. …

Setting Up a Database with MySQL in bash

Finding Files Irrespective of Case in bash
Some of your MP3 files end with .MP3 rather than …

Finding Files Irrespective of Case in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap