Mastering bash scripts

Mastering bash scripts

  • mdo  DigitalBox
  •   Scripting
  •   December 8, 2024

Scripting is a powerful area which let you automate your daily tasks to make your life easier.

In this article, you will learn how to master bash scripts with practical use cases under Haiku.

After that you will never see bash scripting as a strange beast.

Why use bash scripts ?

Most of the time, users are configuring their system according to their wishes : backgrounds, system colors, shortcuts to their preferred applications.

That's easy. What about making their life easier by automating some tasks ?

They do not often think about it, as it's not always straightforward.

That's where bash scripting comes into the picture.

Do you need to automate the launch of your favorite applications ?

Do you want to organize your files in one click ?

Bash, integrated into Haiku, can turn your routines into simple and efficient scripts.

Check your environment

Let's review the basics. Launch a Terminal and type the below :

echo $SHELL

The name of the executable used for the shell should be displayed.

Now let's verify the version by typing :

bash --version

Let's verify how to create a basic bash script.

Launch Pe editor in order to edit a script named "welcome.sh" :

lpe welcome.sh &

Then copy/paste the below text into Pe editor :

#!/bin/bash
echo "Welcome to Haiku !"

If you remember the article "Files permissions", we need to add the "x" permission to the bash script :

chmod +x welcome.sh

The script is now executable :)

Let's launch it :

welcome.sh

It's working fine by displaying a welcome message in the Terminal.

In order to facilitate the access to this script, you will need to place it in the below directory :

/boot/home/config/non-packaged/bin

This directory is important because it's recognized as being in the $PATH variable, meaning all the commands inside will be accessible in a Terminal wherever you are.

Scripts in action

Now let's review a few interesting usages for bash scripting.

Launch several applications in one click

Suppose you have your preferred applications - like StyledEdit and WebPositive - you would like to launch in one click only.

In that case, you only need to edit the below script.

#!/bin/bash
open /boot/system/apps/StyledEdit
open /boot/system/apps/WebPositive

Save it to a script named "launch.sh", add the "x" permission on it, and put it in the "/boot/home/config/non-packaged/bin" folder.

Now, in a Terminal type :

launch.sh

Bingo ! Both StyledEdit and WebPositive are launched together.

You can also add a shortcut of the "launch.sh" script into LaunchBox if you would like to access these applications in one click :

In case you need to have these two applications at the start of the system, just add the call to the "launch.sh" bash script at the end of the below file :

/boot/home/config/settings/boot/UserBootScript

The "UserBootScript" is really useful in that kind of situation :)

Organize files automatically

Now imagine you have configured WebPositive with the download folder set to "/boot/home/Downloads" :

What about organizing your downloaded files depending on their extension ?

For instance all the images files will be placed into the "Images" directory, while the videos will be put into the "Videos" folder.

For that, write the below script "cleanup.sh" :

#!/bin/bash
mkdir -p ~/Documents/Images
mkdir -p ~/Documents/Videos
mv ~/Downloads/*.jpg ~/Documents/Images/ 2> /dev/null
mv ~/Downloads/*.mp4 ~/Documents/Videos/ 2> /dev/null

You can extend the behavior of this script in order to handle more images and videos extensions, as only ".jpg" and ".mp4" are recognized here.

Please note the "-p" option regarding the make directory command (mkdir) : it allows to create the directory in case it doesn't exist.

As usual, make the script executable.

Now in one execution or in one click, your downloaded files will be organized in the correct directory as below :

Nice ?

Notifications when system start

Let's have a bit of fun with notifications under Haiku.

Do you know that fortunes files are available on Haiku under the /system/data/fortunes/ folder ?

It's some various sentences or quotes on a specific domain.

Below are the files available :

Let's select "Art" to make our notifications.

Edit the file "quotes.sh" and copy/paste the below text :

#!/bin/bash
fortune=$(awk -v RS="%" 'BEGIN { srand(); } { blocks[NR] = $0 } END { print blocks[int(rand()*NR)+1] }' /system/data/fortunes/Art)
notify --title "Art" --type "info" "$fortune" --timeout to 5

As usual add the "x" permission to this file. In this script, the "fortune" variable will contain the quote to be used for the notification.

I will not go through the details of the "awk" command here, but the idea is to cut the Art fortune file with the "%" separator, and to select a random sentence.

The notify command, already reviewed in the "Scripting commands" article, will use that fortune sentence and a timeout of 5 seconds will be applied. This timeout can be adjusted of course.

Now launch the script :

quotes.sh

You should see a random art quote as below :

Really fun, no ?

You can of course add this script at the start of the system via the "UserBootScript" file.

Tips for bash scripts

Here are a few tips regarding the bash scripting :

  • In case you do a repetitive task for a command which is not complex enough to create a script, make an alias of that command with all the options needed
  • Add your script to the UserBootScript file in order to execute it at startup
  • Test your script with "bash -x script.sh" to debug it
  • You can use the Haiku "hey" command to make your bash scripts more powerful

I will not give details regarding the "hey" command, as it would require a dedicated article on the subject. But keep in mind that command in the future :)

Final thoughts

After reading this article, you should now be more aware of the benefits to use bash scripting under Haiku.

If you would like to know more on that topic, you can check the "Bash and Scripting" page available on the official Haiku website.

You can also download the "Bash Reference Manual" or the "Hawk Manual" available in PDF format.

As scripting under Haiku can be a powerful area, a new "Scripting" category is now available on this website to collect all the articles relative to scripting.

Do you want to share some interesting scripts ?

Take action ! :)


Powered by Bludit - Hosted by Planet Hoster
© 2025 Haiku Insider