Linux Operating System — Study Notes

Unit 03

Shell Metacharacters & Operators, Filename Expansion, I/O Redirection and the Test Command

4 Questions 5 Marks Each Shell Scripting
01
5 Marks Question

Explain Shell Metacharacters and Operators

Shell Metacharacters

Shell metacharacters are special characters that have a specific meaning to the Linux shell. The shell interprets these characters before passing the command to the operating system. They are also commonly used as wildcards, redirection symbols, command separators, and conditional operators.

Important Shell Metacharacters

MetacharacterPurposeExample
$Variable substitutionecho $USER
>Redirects output to a filels > files.txt
>>Appends output to a filels >> files.txt
<Redirects input from a filewc -l < file.txt
*Matches zero or more charactersls *.txt
?Matches exactly one characterls file?.txt
[ ]Matches characters within a specified range/setls file[1-3].txt
|Sends output of one command to another (pipe)ls | grep txt
;Separates multiple commandsdate; ls
&&Executes next command only if previous succeedsmkdir test && cd test
||Executes next command only if previous failscmd1 || cmd2
&Runs a command in the backgroundfirefox &
#Starts a comment# This is a comment
\Escapes the special meaning of a characterls file\*
Note: The *, ?, and [ ] characters are particularly important for filename expansion (globbing). For example, ls *.txt lists files ending in .txt.

Shell Operators

Shell operators are symbols used to perform calculations, comparisons, logical operations, bitwise operations, and file tests in shell programs. They are classified into five main types: Arithmetic, Relational, Boolean, Bitwise, and File Test operators.

01
Arithmetic Operators

Used for mathematical calculations.

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
++Increment
--Decrement
a=32
b=23

sum=$((a + b))
echo "Sum = $sum"

Output:
Sum = 55

Arithmetic operations are performed using $((...)).

02
Relational Operators

Relational operators compare two values and return true or false.

OperatorMeaning
== / -eqEqual
!= / -neNot equal
< / -ltLess than
<= / -leLess than or equal
> / -gtGreater than
>= / -geGreater than or equal
a=32
b=67

if (( a < b ))
then
    echo "a is less than b"
fi

Symbolic comparisons such as < and > can be used inside ((...)), while forms such as -lt and -gt are used with [ ].

03
Boolean / Logical Operators

These operators perform logical operations.

OperatorMeaning
&& or -aAND — true when both conditions are true
|| or -oOR — true when at least one condition is true
!NOT — reverses the result
a=true
b=false

if [[ $a == "true" && $b == "true" ]]
then
    echo "Both are true"
else
    echo "Both are not true"
fi
04
Bitwise Operators

Bitwise operators perform operations directly on the individual bits of numbers.

OperatorMeaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise Complement
<<Left Shift
>>Right Shift
a=14
b=67

result=$((a & b))
echo "Bitwise AND = $result"

These six bitwise operations are demonstrated using arithmetic expansion.

05
File Test Operators

File test operators are used to check properties and permissions of files.

OperatorPurpose
-bChecks whether the file is a block special file
-sChecks whether the file size is greater than zero
-rChecks whether the file is readable
-wChecks whether the file is writable
-xChecks whether the file is executable
if [ -r file.txt ]
then
    echo "File has read access"
fi
Conclusion Shell metacharacters control how the shell interprets commands, filenames, redirection, and command execution, while shell operators are mainly used for calculations, comparisons, logical decisions, bitwise operations, and file testing. Together, they form an important part of Linux shell scripting.
02
5 Marks Question

Explain Filename Expansion Wildcards

Filename expansion, also called globbing, is the process in which the Linux shell uses special wildcard characters to match a pattern against filenames. The matching filenames are substituted in place of the pattern before the command is executed. Filename expansion is commonly used with commands such as ls, rm, and mv to work with groups of files having similar names.

Wildcards Used in Filename Expansion

01
Asterisk *

The * wildcard matches zero or more characters.

ls *.txt

This displays all files having the .txt extension.

ls file*

This can match files such as file, file1, file123, and file_name.

02
Question Mark ?

The ? wildcard matches exactly one character.

ls file?.txt

It can match file1.txt and file2.txt, but not file10.txt, because file10.txt has two characters after "file".

03
Square Brackets [ ]

Square brackets match one character from the specified set or range.

ls file[123].txt

This matches file1.txt, file2.txt, and file3.txt. A range can also be used:

ls file[a-c].txt

This matches files such as filea.txt, fileb.txt, and filec.txt.

04
Negated Character Set [! ]

[!characters] matches one character that is not included in the specified set.

ls file[!123].txt

This matches a file whose specified character is not 1, 2, or 3.

05
Brace Expansion { }

Braces can be used to specify a list of patterns.

ls file{1,2,3}.txt

This refers to file1.txt, file2.txt, and file3.txt.

ls *.{jpg,png,gif}

This matches files having .jpg, .png, or .gif extensions.

Examples of Filename Expansion

CommandResult
ls *Lists files matching *
ls *.txtLists all .txt files
rm *~Deletes files ending with ~
mv *.{txt,doc} /new/locationMoves .txt and .doc files to the specified location
Special Points: Hidden files begin with . and can be included using patterns such as ls -a *. ls .* can be used to list hidden files. Wildcards do not normally perform recursive matching in subdirectories — more complex matching can be done using tools such as find, or Bash's globstar feature.
Conclusion Filename expansion makes Linux commands more powerful by allowing a single command to operate on multiple files matching a particular pattern. The main wildcards are *, ?, [ ], [! ], and { }.
03
5 Marks Question

Explain Input/Output Redirection in Linux

Redirection is a feature of Linux that allows the standard input or output of a command to be changed from its default device to another file or device.

Standard Input (stdin)
Default device: Keyboard
Standard Output (stdout)
Default device: Screen
Standard Error (stderr)
Default device: Screen

Using redirection, these input/output streams can be redirected to or from files.

01
Output Redirection >

The > symbol is used to redirect standard output (stdout) to a file.

Syntax:
command > filename

Example:
ls -al > listings

The output of ls -al is stored in listings instead of being displayed on the screen.

Note: If the file already exists, its previous contents are overwritten.

02
Append Output >>

The >> operator redirects output to a file without overwriting its existing contents. New output is added at the end.

echo "Hello" >> output.txt

This adds "Hello" to output.txt.

03
Input Redirection <

The < symbol is used for standard input (stdin) redirection. It takes input from a file instead of the keyboard.

Syntax:
command < filename

Example:
wc -l < users

This reads the contents of users as input to wc. The output contains only the line count, without displaying the filename.

04
Error Redirection 2>

Linux uses file descriptor 2 for standard error. The 2> operator redirects error messages to a file instead of displaying them on the screen.

myprogram 2> errorsfile

Here, error messages generated by myprogram are stored in errorsfile, keeping the normal program output separate from error messages.

File Descriptors

Linux associates a File Descriptor (FD) with each open file or stream:

File DescriptorStreamDefault
0Standard Input (stdin)Keyboard
1Standard Output (stdout)Screen
2Standard Error (stderr)Screen
Conclusion Input/output redirection is useful for saving command output, taking input from files, appending data, and separating error messages from normal program output. It is an important feature of Linux shell scripting and command-line operations.
04
5 Marks Question

Explain the Test Command in Detail

The test command in Linux shell scripting is used to evaluate conditions. It is commonly used with if, while, and until statements to perform file, string, and numeric comparisons.

The test command returns an exit status: 0 means the condition is true, and 1 means the condition is false.

Syntax

test condition

Example:
test -f myfile.txt

This checks whether myfile.txt exists and is a regular file.

Types of Tests

01
File Tests

File tests are used to check the properties of files and directories.

OperatorMeaning
-e FILETrue if the file exists
-f FILETrue if the file exists and is a regular file
-d FILETrue if the file exists and is a directory
-s FILETrue if the file exists and has a size greater than zero
-r FILETrue if the file is readable
-w FILETrue if the file is writable
-x FILETrue if the file is executable
if test -f data.txt
then
    echo "File exists"
fi
02
String Tests

String tests are used to compare or check strings.

OperatorMeaning
STRING1 = STRING2Strings are equal
STRING1 != STRING2Strings are not equal
-z STRINGString is empty
-n STRINGString is not empty
if test "$name" = "Faizan"
then
    echo "Name matched"
fi
03
Numeric Tests

Numeric tests are used to compare integer values.

OperatorMeaning
-eqEqual to
-neNot equal to
-gtGreater than
-geGreater than or equal to
-ltLess than
-leLess than or equal to
if test $age -ge 18
then
    echo "Eligible"
fi

These file, string, and numeric conditions are supported by both test and [ ].

[ ] Command

The [ ] command is a synonym for the test command. It provides a more readable syntax for conditional testing.

test -f file.txt

is equivalent to:

[ -f file.txt ]

Important: Spaces must be present around the brackets and the expression.

if [ -f file.txt ]
then
    echo "File exists"
fi
Conclusion The test command is an important shell-scripting command used to evaluate conditions. It can check files, strings, and numbers, and its result can be used with if, while, and until statements to control program execution.