Main points on BASH (Bourne-Again SHell)

Software Engineering Linux

• BASH is a command language interpretor for Linux.
• BASH was developed by GNU Project.
• BASH is backward compatible with sh shell.
• BASH has also incorporated various features from C Shell as well as Korn Shell.

BASH Files

• /etc/profile : It is a systemwide initialization file which is executed when using login shells.
• /etc/bash.bashrc : It is systemwide BASH startup file. Runs when we start BASH for the the first time in our system after turning it on.
• /etc/bash.logout : It is systemwide BASH cleanup file. Runs at the time of closing BASH for cleaning the processes of BASH.
• ~/.bash_profile : It is user-based BASH initialization file.
• ~/.bashrc : It is a pre-interactive shell startup file that runs every time we run BASH.
• ~/.bash_logout : It is cleanup file for every shell we have opened.
• ~/.inputrc : It is the initialization file for each line input in BASH.

BASH Startup Scripts and their execution order

Login shells are those which gets started when we login into a system. Login shells are used to setup an environment for our system. When environment setup is complete, the control is passed to Non-Login shell.

A login shell perform the following on startup,

1. On starting, a login shell calls /etc/profile which runs first every time we login to our system.
2. On succesful execution, the following files are called, *~/.bash_profile, ~/.bash_login & ~/.profile whose roles are described above.
3. The file ~/.bash_profile on running calls ~/.bashrc which in turn further calls /etc/bashrc. Each of them perform different level of initialization as explained.

Non-Login shells are those which are started after we have completed the login procedure for a system.

As the environment for the system is setup by the Login shell itself, so the non login shell perform the following on startup,

1. It calls ~/.bashrc which calls /etc/bash.bashrc which in turn calls /etc/profile.d

The functionality for all the files is proveded above in the section mentioning different BASH files.

BASH Logout Script

When Login Shell exits, BASH exectes the file ~/.bash_logout which logs out the user and calls the /etc/bash.logout for further clearing the whole system.

Except shebang, all lines starting with # are considered as comments in BASH.

Debugging a BASH Script

To debug a shell script, we can use the command

bash -x SCRIPT


Now, both the input as well as output will be visible.

To debug a shell script every time we run it, we can also modify the shebang line and suppy the debugging option there only. The modified shebang is,

#!/bin/bash -x


We can also specify the debugging mode after the shebang line using,

#!/bin/bash
set -x


Converting BASH Scripts to Binary

BASH Scripts can be converted to binaries to hide the functionality of the shell script andprevent users from acessing it to change or modify it's source code.

For conversion of a BASH Script to binary, gcc & shc must be installed on our device.

shc : shc is a command line utility and is a BASH compiler. It is used for the functionality of hiding the sourc code & to remove the dependencies to the file by erging them all into a single gile.

To cnvert the BASH script to binary,

shc -f FILE


The files with the following extenstion will be created,

• .x.c : It is the C code geneated from the BASH script before compiling the code and merging all it's dependency.
• .x : It is a compiled binary file created after successful compilation and no dependency is required at this point. The file can be renamed according to its use.

Kshitiz Saini

(DevOps + Linux) Enthusiast | Bachelor of Technology Computer Science with specialization in DevOps (2018-2022) | Learner | Intern at OpenGenus