Open In App

Bash Pathname Expansion in Linux

Improve
Improve
Like Article
Like
Save
Share
Report

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/*

 


Last Updated : 08 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads