Linux File Globbing Introduction

Linux file Globbing is also known as path name expansion. First we need to know about wildcards then learn about file globbing .

The pattern of wildcards are the strings containing characters like ‘?’, ‘[‘, ‘*’. It is performs action on more than one file having same pattern or to find part of a phrase in a text file.

Shell uses wildcards for file globbing.

File globbing is an operation that recognizes the wildcard pattern and expands it into its path name.

Linux * asterisk

The * asterisk sign is interpreted as the sign to generate matching file names.

It is placed at the end of a line. It is the matches the combination by any number of characters.

Like example of :

# ls office*  

See your terminal after run the command, * It has displayed the matching file names in all the examples.

? question mark

Now you can also use question mark sign in place of asterisk to generate matching file names. It is placed at the end of a line. It is the matches the combination by exactly one character.

Like example of :

# ls office?  

See your terminal after run the command, ? It has displayed the exact matching file names in all the examples.

Linux [] Square Brackets

The Linux [ ] Square brackets are also used to generate matching file names inside the brackets and the first subsequent.

Order inside the square bracket doesn’t matter. It is the matches the combination by exactly one character.

Like example of :

# ls office[A2]  

! exclamation mark

The ( ! ) exclamation mark sign excludes characters from the list within the square bracket. And you can use the combination of asterisk (*), question mark (?) and square bracket [ ].

Like example of :

# ls office[A2][A3]  

See your terminal after run the command, we have used different combinations with exclamation mark.

Ranges [a-z] and [0-9]

Now you can also specify ranges according to your need.

Like example of :

# ls office[a-z]  

Globbing Prevention

echo * will print * when a directory is empty.

But if not empty, them it will print the files.

The special characters sign can be used like backward slash (), single quote (‘) and double quote (“) are show below.

Sample syntax of :

# echo \*  
# echo '*'  
# echo "*"  

See your terminal after run the command, when directory ‘Download’ was empty, * is printed.

When it contains files then list is printed. This ic prevented by using special characters.

Linux Shell Embedding

When you can embed new shells on the command line. It means a command line can have a new shell embedded inside it.

Variables can be use to prove that new shells have been create.

Sample syntax of :

# echo $<variable>  
# echo $<variable>(value)  

Like example of :

# echo $var  
# echo $(var=Hyii ; echo $var)  

See your terminal after run the command, we have embedded a new shell (var-Hyii; echo $var) inside $var.

Requestly note here that $var only exists in the temporary sub shell. That means if you’ll try to print it outside the shell then it will display nothing as shown below.

Backticks

You can use backtick instead of dollar bracket to embed in a command line. Backtick can’t be use to nest embedded shells.

Like example of :

#echo `cd Desktop; ls *.txt | grep file

See your terminal after run the command, we have embedded ‘ls’ and ‘grep’ option in the command line.

grep option is use to search a file matching the specifies pattern. Now e will learn about it in detail in the article.

Difference between backtick(`) and single quote(‘)

The backtick may often confuse with single quote but technically they have significant difference.

Like example of :

# echo `var=Hii; echo $var`  
# echo 'var=Hii; echo $var'  

See your terminal after run the command, backtick embeds the var value in $var.

Whereas, single quote simply echo all the text.

Shell Options

Here, there are two options set and unset and both are built-in commands.

Bash will treat any undefined variable as unbound variable by default.

set -u option, It will treat undefined variables as error.

set +u option, It will display nothing.

If you understand, please practice you own ability.