Shell Scripting is the King of Devlopment


Shell Scripting
Shell scripting is a powerful process of automating day-to-day or regular activities on a Linux computer. It can be implemented on any AWS-hosted Linux machine or a Linux-hosted laptop. Here's an introduction to shell scripting and its commands:
Creating a File in Linux
touch first-shell-script.sh
This command creates a new file named first-shell-script.sh
.
To verify if the file has been created:
ls
The ls
command lists all files and directories in the current directory.
Use ls -ltr
for detailed information about files and directories, including:
Hard links
Owner and group
File size in bytes
Date and time of last modification
Name of the file/directory
To get detailed information about any command, use:
man ls
The man
command provides a manual or description of the specified command.
Editing Files
Use vim
or vi
to edit files. vim
needs to be installed, whereas vi
is pre-installed.
Example:
vim first-shell-script.sh
Inside the editor:
Press
Esc
and theni
to insert text.To save changes, press
Esc
and type:wq!
.To exit without saving, use
:q!
.
To view the file content:
cat first-shell-script.sh
Writing a Basic Shell Script
Start a script with a shebang:
#!/bin/bash
The shebang (#!
) defines the interpreter for the script. Examples include:
/bin/bash
for Bash/bin/sh
for SH/bin/ksh
for KSH
Example script content:
#!/bin/bash
echo "My name is Piyush"
To execute the script:
sh first-shell-script.sh
or
./first-shell-script.sh
Permissions with chmod
The chmod
command changes file permissions. Permissions are divided into three categories:
Root user/administrator
Group
Others
Each permission is represented numerically:
4
= Read2
= Write1
= Execute
Example:
chmod 777 first-shell-script.sh
This grants read, write, and execute permissions to all users.
Useful Commands
mkdir <directory-name>
: Create a directory.cd <directory-name>
: Change directory.rm -rf <file/directory>
: Force delete files or directories.history
: View the history of all executed commands.
To write comments in shell scripts:
# This is a comment
Debugging Mode
Enable debug mode in your script using:
set -x
Example:
set -x
echo "Print disk space"
df -h
echo "Print memory"
free -g
Process Management
ps -ef
: List all running processes with detailed information.ps -ef | grep <keyword>
: Filter processes containing a specific keyword.|
(Pipe): Pass the output of one command as input to another command.
Example:
ps -ef | grep "amazon"
Summary
Creating and editing files: Using
touch
,vim
, andcat
.Permissions: Managed with
chmod
.Debugging and process management: Using
set -x
andps -ef
.Integration: The pipe (
|
) command connects outputs and inputs.
Mastering these commands and techniques makes shell scripting a valuable tool for Linux automation.
Follow me on:
GitHub: https://github.com/piyush-pooh
LinkedIn: https://www.linkedin.com/in/piyush-sharma-5250a0291/
Twitter: https://x.com/Piyush_poooh
Subscribe to my newsletter
Read articles from PIYUSH SHARMA directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

PIYUSH SHARMA
PIYUSH SHARMA
"Passionate DevOps enthusiast, automating workflows and optimizing infrastructure for a more efficient, scalable future."