Bash Pathname Expansion in Linux
Last Updated :
08 Nov, 2022
When you type a command and press enter, bash performs several processes upon the text before carrying out our command as a command. The process that makes this happen is called expansion. For example, suppose you use the echo command for standard output.
echo hi
Output hi
If you use echo with asterisk wildcard echo will print your directory. Suppose you have two files in the current working directory. The first file name is file1.txt and file2.txt then you will use echo with asterisk wildcard then It will print your directory name.
Example:
Types of Expansion
- Pathname Expansion
- Tilde Expansion
- Arithmetic Expansion
- Brace Expansion
- Parameter Expansion
- Command Substitution
Pathname Expansion
The mechanism by which wildcards work is called pathname expansion. you can use wildcard after the filename or before the filename.
echo filestarting_name*
Example:
Tilde Expansion
"~" Tilde Represents the home directory in the Linux system. Use tilde as an expansion
This Command change working directory to home directory
cd ~
This Command change working directory to foo
cd ~/foo
Arithmetic Expansion
The Bash shell allows arithmetic expansion. This allows us to perform a Linux shell as a calculator.
Syntax:
echo $((expression))
Example
echo $((3+3))
Brace Expansion
With this Expansion, you can create multiple text strings from a pattern containing braces
Example:
Parameter Expansion
If you know any programming language basics then parameter expansion is easy for you because it's similar to a data variable. In Linux, You can define a variable like this
# This is $variable_name parameter expansion
name="GFG"
echo $name
GFG
Example:
Mostly Parameter Expansion is used in Bash Script
Command Substitution
Command substitution uses the output of a command as an expansion
Example:
echo $(ls)
file $(ls -d /usr/bin/* | grep zip)
You can also use quotes for command substitution
Example:
ls -l ' which cp'
Quoting
Quoting helps to control Linux command expansion
In this example, You will see your extra space remove by the Linux shell.
$ echo Hi My name is GFG
output : Hi My name is GFG
Output
Double Quotes
Double quotes stop the expansion. If you place characters inside of double quotes, all special characters used by the shell have their special meaning and are treated as ordinary characters. This means some expansions do not work in double quotes example: pathname expansion, tilde expansion, and brace expansion are suppressed.
Example:
Now you can see here that the shell does not remove any space
echo "Hi GFG"
Expected Output: Hi GFG
Output
But Some expansion still works example: arithmetic expansion, and command substitution.
Single Quotes
If you want to suppress all expansions. You can use single quotes.
echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
Expected Output: text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
Escaping Characters
Sometimes we want to quote a single character. To do this, we can precede characters with a backslash called escape characters.
Example
echo "HI \n GFG"
Expected Output: Hi
GFG
Backslash Tricks
Let's take an example, On our desktop, we have a "Backslash Folder" in this folder we also have 1 to 50 folders and the first Folder also has 1 to 50 fifty folders. Sometimes It's very tedious work to type every folder name so we can solve this problem by backslash and asterisk. See the Syntax
Syntax
cd SourceFolder/*/Destination Folder
Example
cd 1/*/50
Output
You can see directory read-write permission without changing the current working directory using backslash
ls -l /directory_name/*
Similar Reads
Bash Brace Expansion In Linux with Examples
BrаÑe exÑаnsiоn is а wаy by whiÑh аrbitrаry strings Ñаn be generаted frоm the terminаl оr by using any bаsh sÑriÑt, it allows you to create multiple modified command-line arguments out of a single argument. The syntаx fоr brаÑe exÑаnsiоn Ñоnsists оf either a sequenÑe sÑeÑifiÑаtiоn оr а cоmmа seÑаrаt
2 min read
Bash Scripting - File Extension
Bash scripting is a powerful tool for automating tasks and working with files in the command line. One important aspect of working with files in bash is understanding how to handle file extensions. In bash, a file extension is the portion of a file name that follows the last period (.) in the file n
6 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if
3 min read
if command in linux with examples
if command in Linux is used for conditional execution in shell scripts.The if command is essential for writing scripts that perform different actions based on different conditions.if COMMANDS list is executed, if its status is true, then the then COMMANDS list is executed. Otherwise, each elif COMMA
4 min read
for command in Linux with Examples
IntroductionThe for command in linux used in shell scripting to iterate over a set of values or perform set of tasks repeatedly. The for loop allows users to automate operations efficiently which is easier to maintain.Syntax: for NAME [in WORDS ... ] ; do COMMANDS; doneHere NAME takes the value of e
2 min read
case command in Linux with examples
The case command in Linux is an essential tool for simplifying script logic, especially when multiple if/elif conditions need to be evaluated for a single variable. It offers a more readable and efficient way to execute commands based on pattern matching, making your shell scripts easier to maintain
2 min read
echo command in Linux with Examples
The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file. Syntax of `echo` command in Linuxecho [option] [string]Here, [option
3 min read
Useful and time saving bash commands in Linux
There are multiple helpful Bash scripts that will make your Linux terminal experience fast and seamless. In Linux, you can automate and execute numerous day-to-day tasks using plain commands â without laying hands on the GUI. These bash script examples assist you in automating file operations, list
5 min read
How To Run Bash Script In Linux?
Bash scripts, also known as shell scripts, are powerful tools in the world of command-line automation. They allow you to perform a series of tasks or execute commands by writing scripts that can be run in a terminal or command-line interface. However, the question often arises: how do you run a Bash
6 min read
read command in Linux with Examples
read command in the Linux system is used to read from a file descriptor. This command reads up the total number of bytes from the specified file descriptor into the buffer. If the number or count is zero, this command may detect errors. But on success, it returns the number of bytes read. Zero indic
3 min read