Linux Shell vs Shell Script – Variables – Basic Operators
Linux Shell vs Shell Script – Variables – Basic Operators
Tram Ho
Hello everyone, in the previous section we have an overview of Shell and Shell Script, if you have not read it, you can see here.
And in this article we will go into more about it. Specifically about what? Let’s start!
Variables
In Linux (Shell), there are 2 types of variable :
System variables: Created and maintained by Linux itself. Usually written in UPPERCASE format.
User-defined variables (UDV): Created and maintained by the user. Usually written in lowercase format.
You can view the system variables by turning on the terminal and typing the set command:
Note: Do not change the System variable, it may cause a system error.
Definition of variables: variable_name=variable_value
variable_name : Must start with alphanumeric characters or underscores. For example: HOME, user_name …
Do not put spaces on either side of the equal sign when assigning values to variables. For example: $no =1 , $no= 1 , $no = 1 .
Variables are case-sensitive, like the filename in Linux.
The variable NULL can be defined as follows: $no=$no=""
Do not use characters ! * - @ $ ... to name variables because they have special meaning for Shell (*).
To see the value of the variable we use the command: echo $VARIABLE_NAME
Readonly variable: its value cannot be changed – readonly variable_name syntax
Discard a variable – Syntax unset variable_name
Special variables (*)
As mentioned above, we cannot use special characters like ! @ $ ... to name the variable. This is because the characters are used in special Linux variables. These variables are reserved for specific functions:
No
Variable & Description
first
$ 0 – Filename of current command / script
2
$ n – These variables correspond to the arguments passed, n is a positive integer. For example: ./test.sh ab , the arguments $ 1, $ 2 are a and b respectively
3
$ # – Number of arguments passed. For example: ./test.sh ab will have 2 arguments
4
$? – The exit status of the previous command is run (usually 0 represents the previous command successfully run, other than 0 is failed) Max range [0 – 255]
5
$$ – Number of current shell processes. For Shell Script, this is the processID number it is running
6
$! – Process number of the last background command
7
$ * – Contains all arguments passed. If there are 3 arguments passed, the value will be $ 1 $ 2 $ 3 when used
8
[email protected] – Contains all arguments passed but separated into separate arguments like $ *
Consider the following example. Create a file with arbitrary name, here I put test.sh with the following content:
Run this file and then observe the result (remember to give this file permission): ./test.sh Hello World
Do you see the variables $* and [email protected] are the same? Basically, these two variables behave the same, unless they are enclosed in “” . When putting quotation marks (“”) then:
$ *: Input arguments will be concatenated into a string and separated by spaces.
[email protected] : The arguments to be included will be distinguished individually.
Consider the following example:
Results of running ./test.sh Hello World :
Basic Operators
Basically Shell Linux uses basic operators like other programming languages like C / C ++, Java …
Arithmetic Operators
Relational Operators
Boolean Operators
String Operators
File Test Operators
a. Arithmetic Operators (Arithmetic Operators)
Shell didn’t originally have any mechanism to perform arithmetic operations, but it used to use external programs like awk or expr to execute.
The following example shows the syntax for adding 2 numbers (create a file named test.sh with the following content):
Run the command ./test.sh we will get the result: 5
The following should be noted in the above operation:
Between operators and expressions, there must be spaces . For example, 2 + 2 is not correct, should be written as 2 + 2 .
The complete expression must be enclosed in <biểu thức> , called the backtick .
Arithmetic operators supported by Shell. Assume variable a = 10, variable b = 20:
Operator
Describe
For example
+
Summation
expr $a + $b Result: 30
–
Subtraction
expr $a - $b Result: -10
*
Multiplication
expr $a * $b Result: 200
/
Division
expr $b * $a Result: 2
%
Division with the remainder
expr $b * $a Result: 0
=
Assignment
a = $b : Assign the value of b to a
==
The comparison is equal to (true, false)
[ $a == $b ] Result: false
! =
Other comparisons (true, false)
[ $a == $b ] Result: true
It is important to note that the conditional expressions must be surrounded by spaces in []. For example [$ a == $ b] is true, and [$ a == $ b] is wrong.
All arithmetic operations are performed using a long interger.
b. Relational Operator (Relational Operator)
Shell supports the following relational operators exclusively for numerical values . These operators do not work for string values unless their value is numeric.
Assume $ a = 10 and $ b = 20:
Operator
Describe
For example
-eq (Equal)
Checks whether the values of 2 operands are equal
[ $a -eq $b ] Result: false
-ne (Not Equal)
Checks whether the values of 2 operands are different
[ $a -ne $b ] Result: true
-gt (Greater than)
Checks whether the value of left operand is greater than the value of right operand
[ $a -gt $b ] Result: false
-lt (Less than)
Checks whether the value of left operand is less than the value of right operand
[ $a -lt $b ] Result: true
-ge (Greater or Equal)
Checks whether the value of left operand is greater than or equal to the value of right operand
[ $a -ge $b ] Result: false
-le (Less or Equal)
Checks whether the value of left operand is less than or equal to the value of right operand
[ $a -le $b ] Result: true
c. Boolean Operators (Boolean Operators)
The following Boolean operators are supported by Shell. Suppose $ a = 10, $ b = 20.
Operator
Describe
For example
!
Negative logic
[! false] Result: true
-o
OR operation – If one of the operands is true then the expression is true
[$ a -lt 20 -o $ b -gt 100] Result: true
-a
AND operation – If all operands are true then the expression is true
[$ a -lt 20 -a $ b -gt 100] Result: false
d. String Operators
Assuming $ a = “abc”, $ b = “efg”:
Operator
Describe
For example
=
Checks whether the values of 2 operands are equal
[$ a = $ b] Result: false
! =
Checks whether the values of 2 operands are different
[$ a! = $ b] Result: true
-z
Check the length operand by 0. not
[-z $ a] Result: false
-n
Check other operand length 0 not
[-n $ a] Result: true
str
Check if string str is different from empty
[$ a] Result: true
e. File Test Operators (File test operators)
Assuming the $ file variable points to a file named “test”, the file size is 100 bytes and has read, write, and execute rights .