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
| Metacharacter | Purpose | Example |
|---|---|---|
| $ | Variable substitution | echo $USER |
| > | Redirects output to a file | ls > files.txt |
| >> | Appends output to a file | ls >> files.txt |
| < | Redirects input from a file | wc -l < file.txt |
| * | Matches zero or more characters | ls *.txt |
| ? | Matches exactly one character | ls file?.txt |
| [ ] | Matches characters within a specified range/set | ls file[1-3].txt |
| | | Sends output of one command to another (pipe) | ls | grep txt |
| ; | Separates multiple commands | date; ls |
| && | Executes next command only if previous succeeds | mkdir test && cd test |
| || | Executes next command only if previous fails | cmd1 || cmd2 |
| & | Runs a command in the background | firefox & |
| # | Starts a comment | # This is a comment |
| \ | Escapes the special meaning of a character | ls file\* |
*, ?, 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.
Used for mathematical calculations.
| Operator | Meaning |
|---|---|
| + | 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 $((...)).
Relational operators compare two values and return true or false.
| Operator | Meaning |
|---|---|
| == / -eq | Equal |
| != / -ne | Not equal |
| < / -lt | Less than |
| <= / -le | Less than or equal |
| > / -gt | Greater than |
| >= / -ge | Greater 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 [ ].
These operators perform logical operations.
| Operator | Meaning |
|---|---|
| && or -a | AND — true when both conditions are true |
| || or -o | OR — 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
Bitwise operators perform operations directly on the individual bits of numbers.
| Operator | Meaning |
|---|---|
| & | 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.
File test operators are used to check properties and permissions of files.
| Operator | Purpose |
|---|---|
| -b | Checks whether the file is a block special file |
| -s | Checks whether the file size is greater than zero |
| -r | Checks whether the file is readable |
| -w | Checks whether the file is writable |
| -x | Checks whether the file is executable |
if [ -r file.txt ]
then
echo "File has read access"
fi
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
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.
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".
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.
[!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.
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
| Command | Result |
|---|---|
| ls * | Lists files matching * |
| ls *.txt | Lists all .txt files |
| rm *~ | Deletes files ending with ~ |
| mv *.{txt,doc} /new/location | Moves .txt and .doc files to the specified location |
. 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.
*, ?, [ ], [! ], and { }.
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.
Using redirection, these input/output streams can be redirected to or from files.
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.
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.
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.
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 Descriptor | Stream | Default |
|---|---|---|
| 0 | Standard Input (stdin) | Keyboard |
| 1 | Standard Output (stdout) | Screen |
| 2 | Standard Error (stderr) | Screen |
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
File tests are used to check the properties of files and directories.
| Operator | Meaning |
|---|---|
| -e FILE | True if the file exists |
| -f FILE | True if the file exists and is a regular file |
| -d FILE | True if the file exists and is a directory |
| -s FILE | True if the file exists and has a size greater than zero |
| -r FILE | True if the file is readable |
| -w FILE | True if the file is writable |
| -x FILE | True if the file is executable |
if test -f data.txt
then
echo "File exists"
fi
String tests are used to compare or check strings.
| Operator | Meaning |
|---|---|
| STRING1 = STRING2 | Strings are equal |
| STRING1 != STRING2 | Strings are not equal |
| -z STRING | String is empty |
| -n STRING | String is not empty |
if test "$name" = "Faizan"
then
echo "Name matched"
fi
Numeric tests are used to compare integer values.
| Operator | Meaning |
|---|---|
| -eq | Equal to |
| -ne | Not equal to |
| -gt | Greater than |
| -ge | Greater than or equal to |
| -lt | Less than |
| -le | Less 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
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.