Customize your computer with bash scripts
Bash scripts − that is, scripts bearing the file-name ending ".sh" offer a convenient
way of writing executable protocols or even customizing your computer to your needs.
In general, bash scripts can contain any command that you could write into a Linux terminal, given that
no SUDO pass word is required for execution. A bash script could thus be used to e.g. launch a program,
open a file, or download and install packages for a programming language like Python. This makes bash
scripts a powerful tool that can make your life as a programmer or data scientist a lot easier.
In our first example, we want to implement a very simple operation: open the FireFox web browser and
then visit the website www.wikipedia.org. This is of course a very simple example, because we could
easily open the program just by clicking on the icon, and then type the website address ourselves.
The usefulness will become apparent later. Let's have a look at a bash script that executes this
firefox -new-tab -url https://www.wikipedia.org
That's it! As you can see, a bash script is very easy to write. The first line − the so-called
shebang line − is mandatory and gives some specifications about how this script should be executed.
The shebang is not exclusive to bash scripts, but is always required for executable scripts, e.g. also for
executable Python scripts, where the line indicates which Python version the script should be executed with
The second line simply states that the Firefox webbrowser should be used to visit the website " wikipedia.org".
The term "-new-tab" is an additional argument that specifies that the website should be opened in a new tab,
which can be convenient if you want to open multiple websites in one Firefox session using a bash script.
The "url" term states that we want to specifically visit a site on the worl-wide web − as you may know,
webbrowser programs can also be used to connect to a local server or to simply display a html file that is stored on your
Here you can also see how arguments are passed to the command line (or terminal): instead of inserting them into brackets
of specific functions, as in e.g. Python or R, the arguments are simply written after the function call, and declared as
argmuents with the minus sign. We can even concatenate a sequence of commands to be executed by Firefox into a single line:
firefox -new-tab -url https://www.wikipedia.org -new -tab https://de.wikipedia.org/wiki/Wikipedia:Hauptseite
This opens the two websites in seperate tabs in one Firefox session.
As you can see, such a bash script could be a convenient way to start a Firefox session accustomed to your preferences
− instead o just launching the standard start page, you can now open your frequently visited websites on start-up!
The final step before the bash scipt can be applied is making it executable. This can be done either by simply right-
clicking on the file, clicking on "properties" and then checking the box next to "allow executing file
as program" under the "Permissions" tab. Or you can open a terminal and do the same via the command line:
Let us now look at a second example: When programming with Python, it is common to work in so-called environments, which
are tailored to zhe requirements of your programming tasks by containing the required packages in the required versions
(environments thus circumvent possible compatibility issues). Suppose you have written an application that requires a
distinct set of Python packages to operate correctly, and you don't want to leave the task of installing them to the
user.Then you could write a bash script that sets up this environment:
yes | conda create -n DLV
conda activate DLV
yes | conda install python=3.6
yes | conda install numpy=1.15.4
yes | conda install scipy=1.1.0
yes | conda install pandas=0.23.4
yes | conda install pillow=5.4.1
yes | pip install dplython
yes | conda install scikit-learn=0.20.2
yes | conda install matplotlib=3.0.2
yes | conda install tensorflow-gpu=1.12.0
yes | conda install keras=2.2.4
yes | conda install spyder
Here, in the second line, the Anaconda program is launched by sourcing a bash script that has been installed as
part of the Anaconda program (Anaconda manages the the creation of environments and the installation of the majority of
In the third line, Anaconda is called to create an environment named "DLV". As you can see, the term "yes |"
is inserted before the actual command. This is done to automatically answer a "yes/no"query. This query is a standard
procedure and has no deeper implications; it is smply used to assert that a given amount of disk space will be used to install
the packages. Since they don't require a lot of space, we can safely answer the query with "yes". In the fourth
line, the environment is activated, and in the following lines, several packages are installed into that environment (in this
example, an environment for Deep-Learning tasks is set up). All these commands would invoke a query concerning usage of disk
space, so we insert the "yes |" flag in front of each.
This file could now be sent to a potential user (who would still need to make the file executable on his or her own computer,
since this attribute is not retained when copying the file). The user would then be able to set up the environment in a very
convenient way that saves a lot of typing time.
Finally, let us look at a somewhat extended application of a bash script: Suppose we have a suite of bash scripts starting programs,
like the one we discussed above. Suppose we would like our computer to start all these programs automatically after booting, Since
we now that our computer is not the fastest one when it comes to starting programs. With what we just learned, we could write a bash
script that calls all the other bash scripts starting their repective program.
Suppose we also want to have the option to not execute these bash scripts. Then we need a user-computer dialogue that is executed in
a terminal. The bash script would then look like this:
gnome-terminal -x bash -c 'echo start by starting programs?; read condition; if [ "$condition" -eq 1 ]; then sh ./start_rstudio.sh & sh ./start_web.sh & bash start_spyder.sh; fi'
There is a lot to see in the second line of code, so let us go through it step by step. "gnome-terminal" opens a terminal,
"bash" starts the bash interpreter that we now require to generate the user dialogue. Essentially, the user dialogue is
contained in between the quotation marks. It consists of the echo command, which prints the subsequent words to the console.Thus The
user will see the words "start by starting programs?" in the terminal. What follows after the semicolon is the command to
initialize a query. In our case, we will use the numbers 1 and 0 as a stand-in for "yes" and "no".
The condition is then evaluated by a "if-then" query, which is similar to the "if" queries in the R and Python
languages (in these, the "then" term is replaced by a bracket pair or by a colon, respectively). If the number entered is
"1", then the bash scriptsa starting the programs will be called; otherwise, nothing will be done. The dialogue is then terminated,
which is declared by the "fi" term. This means that the terminal will be closed once the user has answered the query.
In order to automatically source this bash script after booting, under Ubuntu (version 18), we need to add it to the list of start-up
applications. This is a list of applications that will launch on start-up. The list can be found by typing "start-up applications"
in the search field of the program overview. An entry must then be added by clicking the "add" button; then, the name and location
of the bash script must be provided.
Voila! We have created a convenient script that saves us some waiting time, and thus allows for a short coffee break after booting our
As you can see, bash scripts can indeed help you customize your computer to your needs. The bash synthax itself may not be too easy to learn from scratch, but many useful commands can be easily looked up online using a search engine, as in all programming-related issues.
The examples shown here are of course just a small part of the possibilities that these scripts provide. So what are you waiting for? Go ahead
and start implementing your own applications with bash!