How to Write Script in Bash?

In this guide, I'll show you how to write script in Bash. You can also find several useful examples of bash scripts. So at the end you will get:

  • how to write a script in bash?
  • what is bash
  • bash script examples

What is Bash?

A script in Bash (short for "Bourne-Again shell") is a program written in the Bash programming language. It's mainly used to automate tasks and perform system administration tasks on Linux, macOS, and other Unix-like operating systems.

Here's an example of a simple Bash script that prints: "Hello, World!" to the screen:

#!/bin/bash
echo "Hello, World!"

This script starts with a shebang line - #!/bin/bash. This line specifies the interpreter to use for the script (in this case, /bin/bash).

The second line uses the "echo" command to print the string "Hello, World!" to the terminal.

Write Bash script

Here are the steps to create a Bash script:

  • Create a new file (using a text editor such as nano, vim or emacs)
    • sudo nano test.sh
  • Start by shebang line "#!/bin/bash" (always on the top of the file)
  • Write your commands, one per line
  • Save the file and exit the editor
    • CTRL + X + (y) - for nano
Note:

Note that the script that you create, needs to be saved with the .sh extension, so that the operating system can recognize it as a shell script..

Run Bash script

First we need to make the file executable by running the command:

chmod +x test.sh

Run the script by typing

./test.sh

Bash commands

There are useful commands like:

  • 'if'
  • 'for'
  • 'while'
  • 'case'

to add logic to your script. The script can also take command line arguments, which can be accessed by using the special variables $1, $2, $3, etc, in the script.

Bash script examples

Stop program after X hours

The first example is a script which stops given program in X hours:

#!/bin/bash
date '+%A %W %Y %X'
sleep 2.0h
pkill chrome

Stop program in X min (user input)

Next script shows how to stop the program in X minutes. The X is taken as user input from the terminal:

#!/bin/bash
read -p "How long to delay (60 - 60m)? " answer

echo "Now:" $(date +"%H:%M")
echo "Will end:" $(date -d "+ $answer minute" +'%H:%M')

delay=$(echo "$answer * 60" | bc)
sleep $delay

pkill chrome

Run random song each hour

The next script is starting a program called rhythmbox with a random song from a given folder.

#!/bin/bash
export XDG_RUNTIME_DIR="/run/user/1000"
MUSIC=/mnt/x/Music/
cd $MUSIC
#ls |sort -R |tail -1 |while read file; do
#    echo "$MUSIC$file"
#	rhythmbox "$MUSIC$file" & sleep 5m
#done
FILE_NAME=$(find /mnt/x/Music/ -type f -iname "*.mp3" | shuf -n1)
echo $FILE_NAME
rhythmbox  "$FILE_NAME"  & sleep 5m

killall rhythmbox

You need to setup also a CRON job to run this script - motivation.sh:

  • Type crontab -e
  • enter the command:
    • 55 9-18 * * * DISPLAY=:0 /home/user/Scripts/cron/motivation.sh

This will run the script from 9 to 18 starting at 55-th minute.

How the script works:

  • export XDG_RUNTIME_DIR="/run/user/1000" - workaround for rhythmbox
  • MUSIC=/mnt/x/Music/ - select the music folder
  • cd $MUSIC - go in the folder
  • find random song in 2 different ways
    • '#' - this is a commented line
  • echo $FILE_NAME - print the name of the selected song
  • start the rhythmbox player and wait 5 minutes
  • finally stop the rhythmbox program