Unix / Linux - Shell Basic Operators
Hello, aspiring programmers! I'm thrilled to guide you through the fascinating world of Unix/Linux shell operators. As someone who's been teaching computer science for over a decade, I can assure you that mastering these operators will be a game-changer in your programming journey. Let's dive in!
Arithmetic Operators
Arithmetic operators are the building blocks of mathematical operations in shell scripting. They allow us to perform basic calculations within our scripts.
Here's a table of the most common arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Addition | expr $a + $b |
- | Subtraction | expr $a - $b |
* | Multiplication | expr $a \* $b |
/ | Division | expr $a / $b |
% | Modulus | expr $a % $b |
= | Assignment | a=$b |
Let's look at a practical example:
#!/bin/bash
a=10
b=5
echo "Addition: `expr $a + $b`"
echo "Subtraction: `expr $a - $b`"
echo "Multiplication: `expr $a \* $b`"
echo "Division: `expr $a / $b`"
echo "Modulus: `expr $a % $b`"
In this script, we're using the expr
command to perform arithmetic operations. The backticks () are used to execute the
expr` command and substitute its output.
When you run this script, you'll see:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
Remember, in shell scripting, spaces are crucial. expr$a+$b
won't work, but expr $a + $b
will. It's like giving your variables room to breathe!
Relational Operators
Relational operators are used to compare values. They're essential for creating conditions in your scripts.
Here's a table of relational operators:
Operator | Description | Example |
---|---|---|
-eq | Equal to | [ $a -eq $b ] |
-ne | Not equal to | [ $a -ne $b ] |
-gt | Greater than | [ $a -gt $b ] |
-lt | Less than | [ $a -lt $b ] |
-ge | Greater than or equal to | [ $a -ge $b ] |
-le | Less than or equal to | [ $a -le $b ] |
Let's see these in action:
#!/bin/bash
a=10
b=20
if [ $a -eq $b ]
then
echo "$a is equal to $b"
elif [ $a -gt $b ]
then
echo "$a is greater than $b"
else
echo "$a is less than $b"
fi
This script compares a
and b
using relational operators. When you run it, you'll see:
10 is less than 20
Pro tip: Think of these operators as asking questions. "-eq" is like asking "Are these equal?", "-gt" is asking "Is this greater than that?". It helps to verbalize these comparisons when you're writing your scripts.
Boolean Operators
Boolean operators allow us to combine multiple conditions. They're like the logical glue of your scripts.
Here's a table of boolean operators:
Operator | Description | Example |
---|---|---|
! | NOT | [ ! false ] |
-o | OR | [ $a -lt 20 -o $b -gt 100 ] |
-a | AND | [ $a -lt 20 -a $b -gt 100 ] |
Let's use these in a script:
#!/bin/bash
a=10
b=20
if [ $a -lt 20 -a $b -gt 15 ]
then
echo "Both conditions are true"
else
echo "At least one condition is false"
fi
When you run this script, you'll see:
Both conditions are true
I like to think of boolean operators as decision-making tools. They help your script make choices based on multiple factors, just like we do in real life!
String Operators
String operators are used to compare and manipulate text strings. They're incredibly useful when working with text data.
Here's a table of string operators:
Operator | Description | Example |
---|---|---|
= | Equal to | [ $a = $b ] |
!= | Not equal to | [ $a != $b ] |
-z | String is null | [ -z $a ] |
-n | String is not null | [ -n $a ] |
str | String is not empty | [ $a ] |
Let's see these in action:
#!/bin/bash
a="Hello"
b="World"
if [ $a = $b ]
then
echo "Strings are equal"
elif [ -z $a ]
then
echo "String a is empty"
elif [ -n $b ]
then
echo "String b is not empty"
else
echo "None of the above conditions are true"
fi
When you run this script, you'll get:
String b is not empty
String operators are like the grammar rules of shell scripting. They help you make sense of the text your script is working with.
File Test Operators
File test operators are used to test different file conditions. They're crucial when your script needs to interact with files and directories.
Here's a table of some common file test operators:
Operator | Description | Example |
---|---|---|
-d file | Directory exists | [ -d $file ] |
-f file | File exists and is a regular file | [ -f $file ] |
-r file | File is readable | [ -r $file ] |
-w file | File is writable | [ -w $file ] |
-x file | File is executable | [ -x $file ] |
-s file | File is not zero size | [ -s $file ] |
Let's use these in a script:
#!/bin/bash
file="/etc/passwd"
if [ -f $file ]
then
echo "File exists"
if [ -r $file ]
then
echo "File is readable"
fi
if [ -w $file ]
then
echo "File is writable"
fi
if [ -x $file ]
then
echo "File is executable"
fi
else
echo "File does not exist"
fi
When you run this script, you might see something like:
File exists
File is readable
File test operators are like your script's file detective tools. They help your script understand what it can and can't do with different files.
C Shell Operators
The C Shell (csh) has its own set of operators that are slightly different from the Bourne Shell. Here are some examples:
#!/bin/csh
set a = 10
set b = 20
if ($a == $b) then
echo "a is equal to b"
else if ($a != $b) then
echo "a is not equal to b"
endif
if ($a > $b) then
echo "a is greater than b"
else if ($a < $b) then
echo "a is less than b"
endif
In C Shell, we use ==
for equality and !=
for inequality. Also, we can directly use >
and <
for comparisons.
Korn Shell Operators
The Korn Shell (ksh) combines features from both the Bourne Shell and the C Shell. Here's an example:
#!/bin/ksh
a=10
b=20
if ((a == b))
then
print "a is equal to b"
elif ((a != b))
then
print "a is not equal to b"
fi
if ((a > b))
then
print "a is greater than b"
elif ((a < b))
then
print "a is less than b"
fi
In Korn Shell, we can use ((
and ))
for arithmetic operations and comparisons, which makes the syntax cleaner and more intuitive.
Remember, practice makes perfect! Don't be afraid to experiment with these operators in your own scripts. Each time you use them, you'll become more comfortable and proficient. Happy scripting!
Credits: Image by storyset