Linux: Learn Basic Bash Scripting

Linux logo

Learn the Basics of Bash Scripting

What is Bash Scripting?

Bash scripting refers to the process of writing and running scripts using the Bash (Bourne-Again Shell) shell, which is a popular Unix/Linux shell and command language. Bash is the default shell for many Unix-based operating systems, including Linux and macOS. Bash scripts are a series of commands and instructions written in the Bash language, which are executed sequentially when the script is run.

Bash scripting is commonly used for various purposes, including:

  • Automation: You can use Bash scripts to automate repetitive tasks, such as system maintenance, file operations, backups, and more.

  • System Administration: Bash scripts are widely used for system administration tasks, like user management, software installation, and monitoring.

  • Customization: You can create custom scripts to configure your system, set environment variables, and customize your command-line environment.

  • Data Processing: Bash scripts are useful for processing and manipulating data, such as log files, text files, and CSV files.

  • Interactive Command-Line Tools: Bash scripts can be used to create interactive command-line tools, providing user-friendly interfaces for various tasks.

Bash scripts are written in a simple and readable syntax, making them accessible to both beginners and experienced programmers. They can include control structures (such as loops and conditionals), variables, functions, and command substitution. Bash also supports input/output redirection, piping, and regular expressions, which further enhance the scripting capabilities.

Bash Scripting Cheet Sheet


This line at the beginning of your script specifies the interpreter to use (in this case, /bin/bash).

# This is a comment
echo "This is a message"
variable_name="Hello, World"
if [ condition ]; then
    # Code to execute if the condition is true

if [ condition ]; then
    # Code to execute if the condition is true
    # Code to execute if the condition is false
# For loop
for item in item1 item2 item3; do
    # Code to execute for each item

# While loop
while [ condition ]; do
    # Code to execute while the condition is true
function my_function() {
    # Function code
my_function  # Call the function
read user_input
Command Line Arguments:
$0  # Script name
$1  # First argument
$2  # Second argument
$#  # Number of arguments
$@  # All arguments as an array
File Operations:
# Check if a file exists
if [ -e "file.txt" ]; then

# Check if a file is a directory
if [ -d "directory" ]; then

# Check if a file is readable, writable, or executable
if [ -r "file.txt" ]; then
if [ -w "file.txt" ]; then
if [ -x "" ]; then

# Copy, move, and remove files
cp source_file destination
mv source_file destination
rm file_to_remove
Pipes and Redirection:
# Pipe output of one command to another
command1 | command2

# Redirect standard output to a file
command > output.txt

# Redirect standard error to a file
command 2> error.txt

# Append output to a file
command >> output.txt

# Combine standard output and error
command > log.txt 2>&1
Exit Status:
if [ $? -eq 0 ]; then
    # Command was successful
    # Command failed
Environment Variables:
$HOME    # User's home directory
$USER    # Current user's username
$PATH    # List of directories to search for executable files

This cheat sheet covers the basics of Bash scripting. Bash offers many more features and commands, but these are a good starting point for writing simple scripts.

Here’s a simple example of a Bash script that greets the user:


echo "Hello, what is your name?"
read name
echo "Nice to meet you, $name!"

To execute a Bash script, you typically make it executable using the chmod +x command to make it executable and then you can run the script by typing its name:

chmod +x


Bash scripting is highly versatile, allowing you to automate repetitive tasks, customize your environment, and create powerful system administration tools. It is widely used in various domains, including software development, system administration, and data processing.

That’s All Folks!

You can find all of our Linux guides here: Linux Guides

Luke Barber

Hello, fellow tech enthusiasts! I'm Luke, a passionate learner and explorer in the vast realms of technology. Welcome to my digital space where I share the insights and adventures gained from my journey into the fascinating worlds of Arduino, Python, Linux, Ethical Hacking, and beyond. Armed with qualifications including CompTIA A+, Sec+, Cisco CCNA, Unix/Linux and Bash Shell Scripting, JavaScript Application Programming, Python Programming and Ethical Hacking, I thrive in the ever-evolving landscape of coding, computers, and networks. As a tech enthusiast, I'm on a mission to simplify the complexities of technology through my blogs, offering a glimpse into the marvels of Arduino, Python, Linux, and Ethical Hacking techniques. Whether you're a fellow coder or a curious mind, I invite you to join me on this journey of continuous learning and discovery.

One thought on “Linux: Learn Basic Bash Scripting

  1. F*ckin’ awesome here. I’m very satisfied to see your post. Thanks so much and i am having a look forward to contact you. Will you please drop me a e-mail?

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights