- Introduction to Shell Scripting notions
Shell scripting is a method of automating tasks in Unix/Linux operating systems by writing a sequence of commands inside a script file. A shell script allows system administrators, DevOps engineers, and developers to automate repetitive operations such as system administration, file management, software installation, backups, monitoring, and application deployment.
A shell script is executed by a shell interpreter, such as Bash (Bourne Again Shell), which reads commands from a file and executes them automatically.
Shell scripting improves productivity by reducing manual work, increasing reliability, and enabling efficient system management.
Common Linux shells include:
Bash (Bourne Again Shell) – Most widely used Linux shell Bash is commonly used in DevOps because of its compatibility and powerful scripting features.
Example:
#!/bin/bash
echo "Hello DevOps World"
Output:
Hello DevOps World
Create a file:
nano script.sh
Add:
#!/bin/bash
echo "My first shell script"
Give execution permission:
chmod +x script.sh
Run the script:
./script.sh
- Shebang (#!)
The shebang defines which interpreter should execute the script.
Example:
#!/bin/bash
It tells Linux to execute the script using Bash.
Other examples:
#!/bin/sh #!/usr/bin/python3
- Variables in Shell Scripting
Variables store information that can be reused inside scripts.
Example:
#!/bin/bash
name="DevOps"
echo "Welcome to $name"
Output:
Welcome to DevOps Types of Variables User-defined variables server="production"
echo $server System variables
Examples:
echo $USER echo $HOME echo $PWD echo $HOSTNAME
Output example:
Hp /home/user /home/user/project linux-server 5. User Input
Shell scripts can receive input from users.
Example:
#!/bin/bash
echo "Enter your name:" read username
echo "Hello $username"
Execution:
Enter your name: John
Hello John