In Shell, we use variable structure as a little container boxes where you can store the values you want to execute multiple times in your script. And the data type is something where you can use variables to present data using quotes ‘ ‘ and ” ” (single and double quotes). Double and single quotes have their specific data structuring methods, which is what we will understand in this article. We will understand shell scripting variables and data types.
Why did we use variables, and how are they useful to us?
Languages like Perl, Python, C, Java,
and shell scripts
use variables. The variables can make tasks easy and make multiple changes less of a hassle in the future.
The reason for using variables is to assign a value to the script’s multiple directions, making it easy to make changes in the future. For example, if you are using VERSION=’1.1′ And you put in every part of the script, then it just needs to change the top variable from 1.1 to 1.2 or anything else.
Here are some example variables:
!/bin/bash
VAR1='1.1'
DATE=$(date +%Y%m%d)
sudo yum install pachage-"$VAR1" -y
cp /etc/pachake-"$VAR1".conf /tmp/pachake-"$VAR1"-"$DATE".conf.backup
Explanation:
- This script automates the installation of a package version (1.1) and backs up its configuration file.
- The original config file is backed up to /tmp with a date-stamped filename.
- VAR1 holds the version number.
- DATE stores the current date in YYYYMMDD format.
- The package is installed using yum.
Note: Never keep important data in /tmp. It is just a temporary path that may cause data loss after reboot.
What are data types?
The data type in a shell script shows what kind of data stored inside the variable. Generally, a shell script contains simple text-based data in variables, which makes it simpler to use. Data types somehow show the value against the variable, and there are so many data types available. Let’s see some examples here.
Different Types of Data Types:
1. Strings
- Text enclosed in quotes.
- Default type when assigning plain text.
NAME="Rushikesh"
echo "Hello, $NAME"
2. Integers (for arithmetic)
- Bash supports integer arithmetic using
let
,(( ))
, orexpr
.
NUM1=5
NUM2=3
let SUM=NUM1+NUM2
echo "Sum is $SUM"
Or:
SUM=$((NUM1 + NUM2))
3. Command Substitution
- Stores the result of a command in a variable.
TODAY=$(date +%A)
echo "Today is $TODAY"
4. Arrays
- A variable that can hold multiple values.
FRUITS=("apple" "banana" "cherry")
echo "${FRUITS[1]}" # Outputs: banana
5. Boolean (simulated)
- Bash doesn’t have a native boolean type, but you can simulate it using
0
(true) and1
(false) in conditions.
IS_ACTIVE=0
if [ "$IS_ACTIVE" -eq 0 ]; then
echo "Active"
else
echo "Inactive"
fi
Conclusion:
In shell scripting, variables and data types play a crucial role in writing efficient and reusable code. They allow you to store values, simplify updates, and automate repetitive tasks. Understanding how to use quotes and data types like strings, integers, arrays, and command substitution enhances your scripting capabilities. Even though Bash lacks strict typing, simulating types like booleans helps in managing conditions effectively. Mastering these basics lays the foundation for writing powerful and maintainable shell scripts.
KWUodzG texrqV kZEUox XgRTgeNF ohiJe pyfO YPjt
Jvel XKhsT XSJUJu IgGqwIPf unVILfSF