If you’ve ever worked in a tech or programming field, there’s a good chance you’ve used Bash. It’s one of the most common command-line interpreters available today. While it can seem daunting at first, Bash is actually quite simple to use – with just a few basic commands, you can start performing complex tasks and get work done more quickly. In this blog post, we’ll show you the basics of using Bash, so you can start using it yourself.
Continue reading to learn everything you need to know about getting started with Bash!
Simple Script to Print Hello World
#!/bin/bash
echo "Hello, world!"
Script to Print the Date and Time
#!/bin/bash
date
Script to Print the Current Working Directory
#!/bin/bash
pwd
Script to List the Contents of a Directory
#!/bin/bash
ls -l
Script to Rename a File
#!/bin/bash
mv oldfile.txt newfile.txt
Script to Copy a File
#!/bin/bash
cp file1.txt file2.txt
A Bash Script with Arguments
#!/bin/bash
echo "My first argument is: $1"
echo "My second argument is: $2"
echo "All my arguments are: $*"
A Bash Script that Reads from STDIN
#!/bin/bash
read -p "Enter your name: " name
echo "Hello, $name!"
A Bash Script that Uses Variables
#!/bin/bash
name="John"
echo "Hello, $name!"
A Bash Script that Uses an Array of Values
#!/bin/bash
names=("John" "Jane" "Joe")
echo "Hello, ${names[0]}!"
echo "Hello, ${names[1]}!"
echo "Hello, ${names[2]}!"
A Bash Script that Uses a For Loop to Iterate over an Array of Values
#!/bin/bash
names=("John" "Jane" "Joe")
for name in "${names[@]}"
do
echo "Hello, $name!"
done
A Bash Script that Uses a While Loop to Read from STDIN
#!/bin/bash
while read -r line; do
echo "You entered: $line"
done < /dev/stdin
A Bash Script that Uses an If Statement
#!/bin/bash
read -p "Enter a number: " num
if ((num == 10)); then
echo "$num is 10"
elif ((num > 10)); then
echo "$num is greater than 10"
else
echo "$num is less than 10"
fi
A Bash Script that Uses a Case Statement
#!/bin/bash
read -p "Enter a color: " color
case $color in
red)
echo "$color is red";;
blue)
echo "$color is blue";;
green)
echo "$color is green";;
*)
echo "$color is not red, blue, or green";;
esac
A Bash Script that Calls Another Bash Script
#!/bin/bash
./myscript.sh arg1 arg2 arg3
A Bash Script that Exits with an Error Code
#!/bin/bash
exit 1
A Bash Script that Uses a Function
#!/bin/bash
myfunc() {
echo "Hello, World!"
}
myfunc
Reading Files
Reading files is a common task in Bash scripts. The simplest way to read a file is with the cat command:
cat my_file
This will print the contents of my_file to stdout (the terminal). If you want to store the contents of the file in a variable, you can use the $(<)
operator:
my_variable=$(<my_file)
Another common way to read files is with the while loop:
while read -r line; do
echo "$line";
done < my_file
This will read each line of my_file into the variable line and print it to stdout.
Writing Files
Writing files is just as easy as reading them! The simplest way to write to a file is with the >
operator: echo "Hello, World!" > my_file
This will overwrite any existing content in my_file with “Hello, World!”.
If you want to append text to a file instead of overwriting it, you can use the >>
operator: echo "Hello, World!" >> my_file
Parsing Command-Line Arguments
It’s often useful to be able parse command-line arguments in your Bash scripts.
The most common way to do this is with the getopts
builtin command:
while getopts 'a:' opt; do
case $opt in
a) arg="$OPTARG" ;;
\?) exit 1 ;;
esac
done
Print Exit Status
Every Linux command returns an exit status when it terminates.
The exit status is an integer between 0 and 255 that indicates whether or not the command was successful. You can check the exit status of a command with the $?
special variable:
ls non-existent-directory && echo "Command succeeded!" || echo "Command failed!"
In this example, ls will return a non-zero exit status if the non-existent-directory does not exist, so echo “Command failed!” will be executed.
Signal Handling
When a process receives a signal, it can either ignore it or handle it gracefully by doing some cleanup before exiting.
For example, when you press Ctrl+C (SIGINT), your Bash script will receive this signal and terminate immediately unless you have registered a signal handler for it:
trap 'echo "SIGINT received!"' SIGINT # Register SIGINT handler
# Some code here...
When your script receives SIGINT (e.g., when you press Ctrl+C), it will print SIGINT received! and continue running normally rather than terminating immediately
If you want to improve your system administration or programming skills, learning some basic Bash shell commands is a great place to start. The Bash shell is available on almost all Unix and Linux systems, so once you’ve learned the basics you can apply your skills on almost any platform. Plus, using the command line interface can be much faster and more efficient than working with graphical user interfaces. If you’re interested in learning more about Bash, check out our other blog posts or contact me directly. I’d be happy to help get you started.