Skip to main content
ubuntuask.com

Back to all posts

How Does Bash Handle Pattern Matching Of '*'?

Published on
3 min read
How Does Bash Handle Pattern Matching Of '*'? image

Best Books on Bash Scripting to Buy in October 2025

1 Black Hat Bash: Creative Scripting for Hackers and Pentesters

Black Hat Bash: Creative Scripting for Hackers and Pentesters

BUY & SAVE
$40.39 $59.99
Save 33%
Black Hat Bash: Creative Scripting for Hackers and Pentesters
2 The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting

The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting

BUY & SAVE
$27.89 $49.99
Save 44%
The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting
3 Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

  • AFFORDABLE PRICES FOR QUALITY SECONDHAND READING MATERIAL.
  • ECO-FRIENDLY CHOICE: PROMOTE SUSTAINABILITY WITH USED BOOKS.
  • UNIQUE SELECTIONS: DISCOVER RARE FINDS AND HIDDEN GEMS!
BUY & SAVE
$22.15 $44.99
Save 51%
Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))
4 Classic Shell Scripting

Classic Shell Scripting

BUY & SAVE
$25.73 $49.99
Save 49%
Classic Shell Scripting
5 The Linux Command Line, 2nd Edition: A Complete Introduction

The Linux Command Line, 2nd Edition: A Complete Introduction

BUY & SAVE
$19.58 $39.99
Save 51%
The Linux Command Line, 2nd Edition: A Complete Introduction
6 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
7 The Book of Batch Scripting: From Fundamentals to Advanced Automation

The Book of Batch Scripting: From Fundamentals to Advanced Automation

BUY & SAVE
$59.99
The Book of Batch Scripting: From Fundamentals to Advanced Automation
8 Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

BUY & SAVE
$23.74 $39.99
Save 41%
Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali
9 Bash Pocket Reference: Help for Power Users and Sys Admins

Bash Pocket Reference: Help for Power Users and Sys Admins

BUY & SAVE
$17.60 $24.99
Save 30%
Bash Pocket Reference: Help for Power Users and Sys Admins
+
ONE MORE?

In Bash, the asterisk (*) is known as a wildcard character that can be used for pattern matching in file and directory names. When the asterisk is used in a command, it represents any sequence of characters or none at all.

For example, if you want to list all files in a directory that start with "test", you can use the command "ls test*". This will match all files that start with "test" followed by any characters.

It's important to note that the asterisk can match multiple characters, so it's possible to match more than one file with a single wildcard. Additionally, if you want to match a literal asterisk character in a filename, you can escape it using a backslash (*).

Overall, Bash provides powerful pattern matching capabilities using the asterisk wildcard character, allowing you to easily manipulate and work with files and directories based on specific patterns.

How to perform pattern matching using '*' in bash?

Pattern matching using '*' in bash can be done using the case statement. Here's an example:

#!/bin/bash

read -p "Enter a string: " input

case $input in hello*) echo "The string starts with 'hello'" ;; *world) echo "The string ends with 'world'" ;; *foo*) echo "The string contains 'foo'" ;; *) echo "No matches found" ;; esac

In the above script, the case statement is used to check the input string against different patterns. The '*' character is used as a wildcard to match any sequence of characters.

You can add more patterns to match against the input string by adding more pattern) in the case statement. The *) at the end of each pattern block is used as a fallback for cases where none of the specified patterns match.

You can run this script in your terminal and enter different strings to see how the pattern matching works using '*'.

How to escape the '*' character in bash for literal matching?

To escape the * character in bash for literal matching, you can use a backslash \ before the * character. Here is an example:

ls \*

This command will list all files and directories in the current directory that have a literal * character in their name. The backslash escapes the * character so that it is treated as a literal character and not as a wildcard for pattern matching.

What is the significance of '*' in bash commands?

In bash commands, the '' symbol is used as a wildcard character. It represents zero or more characters in a file name or a string of text. When used in commands like 'ls' or 'rm', the '' symbol allows users to perform operations on multiple files that match a certain pattern or criteria. For example, 'ls *.txt' would list all files with a .txt extension, and 'rm .log' would delete all files with a .log extension. The '' symbol is a powerful tool for performing operations on multiple files quickly and efficiently in the bash shell.