I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…
Working with Shell Arithmetic and Boolean Operators in Unix:
In this tutorial, we will review the various operators that are supported by the Unix shell.
Operators are used for manipulating variables and constants in shell programs. They are required to perform mathematical operations.
Here, we will explain to you more about working with Arithmetic Operators.
Unix Video #14:
Note that the back-tick (`) is often used here – when executing a command, everything between the back-ticks is executed and substituted with the result before the remainder of the command is executed.
In newer shells (Example: bash), the same result can be achieved by embedding the expression between ‘$(’ and ‘)’.
Operators in Unix
#1) Shell Arithmetic Operators Example
These consist of basic mathematical operations:
Addition: +
Subtraction: –
Multiplication: *
Division: /
Modulus: %
Each of these operators operates on two integer variables or constants.
For Example, the below program illustrates each of these operations:
$ c=`expr $a + $b`
$ echo “the value of addition=$c”
$ d=`expr $a - $b`
$ echo “the value of subtraction=$d”
$ e= expr $a \* $b`
$ echo “the value of multiplication=$e”
$ f=`expr $a / $b`
$ echo “the value of division=$f”
$ g= echo `expr $a % $b`
$ echo “the value of modulus=$c”
The Unix shell does not natively support floating-point operations. A separate command-line tool must be used for this. The ‘bc’ co0mmand is the most standard tool for this.
Example:
$ c = `echo “$a + $b” | bc`
$ d = `echo “$a + $b” | bc`
Note that each of the operators needs to be surrounded by a space on both sides and the ‘*’ operators need to be escaped with a backslash ‘\’.
#2) Shell Logical Boolean Operators Example
The logical operators in Unix are as follows:
Not:!
And: -a
Or: -o
These operators and their usage will be covered in detail in the next tutorial.
Introduction to Unix Shell Scripting: In Unix, the Command Shell is the native command interpreter. It provides a command line interface for the users to interact with the operating system. Unix commands may also be executed non-interactively in the form of a Shell Script. The script is a series of…
Overview of Unix Filters Text Processing Utilities: In this tutorial, we will learn about filters and work with various filter commands. Filters are commands that read input from stdin and write output to stdout. By default, when using a shell terminal, the stdin is from the keyboard, and the stdout is…
learn Cut Command in Unix with Simple and Practical Examples: Unix provides a number of filter commands that can be used for processing flat file databases. These filter commands can be chained together to perform a series of operations with a single command. A flat file database is a file…
In this tutorial, you will learn different basic and advanced Unix Commands. Unix commands are inbuilt programs that can be invoked in multiple ways. Here, we will work with these commands interactively from a Unix terminal. A Unix terminal is a graphical program that provides a command-line interface using a…
Learn Unix Cat Command with Examples: The cat command is perhaps the most commonly used Unix command. It is derived from catenate – which describes the process of connecting things. The cat command is a filter that can be used for multiple purposes: Display the contents of text files. Copy text…
Conditional Statements Coding in Unix: Shell scripts often need to be constructed to execute different instructions depending on the value of specific control variables. The different paths of execution are specified using conditional instructions. In this tutorial, we will see about relational operators, and shell decision-making using various conditional statements. …
Overview of Unix Shell Functions: Shell Functions are used to specify the blocks of commands that may be repeatedly invoked at different stages of execution. The main advantages of using Unix Shell Functions are to reuse the code and to test the code in a modular way. This tutorial will…
Learn Tar Command in Unix with practical Examples: The primary function of the Unix tar command is to create backups. It is used to create a ‘tape archive’ of a directory tree, that could be backed up and restored from a tape-based storage device. The term ‘tar’ also refers to the…