bash is the abbreviation of Bourne Again Shell.
It is a commonly-used shell in many Linux distributions. Bash is a command interpreter. It is a command-line–only interface containing a handful of built-in commands; it has the ability to launch other programs and to control programs that have been launched from it (job control).
Each time you log in into a Linux session a file called .bashrc is run. This file is located in your home directory and is installed when installing the Linux distro.
There's quite a lot of stuff inside the default .bashrc file which you better not touch, unless you know what you're doing (like in so many cases...).
However, you can add stuff that benefits your use of Linux. One of them is to add calls to other files that can contain different things like:
aliases
path extensions
exports
...
To do this, follow the steps below:
Open the file ~/.bashrc using vi (or another editor)
Somewhere at the end of the file, add the following 3 commands (YMMV):
[ -f ~/.bash_aliases ] && . ~/.bash_aliases
[ -f ~/.bash_paths ] && . ~/.bash_paths
[ -f ~/.bash_exports ] && . ~/.bash_exports
Save the file
What do those 3 commands do exactly?
They first check if the file given is existing. Only then, the command following the && command is executed. That command is then just sourcing what is in it. That's it, nothing complicated.
An alias file can contain shortcuts (or "aliases") to more elaborated commands. Mine currently looks as follows:
alias lla='ls -als'
alias ltr='ls -ltr'
alias temp='/opt/vc/bin/vcgencmd measure_temp'
So, each time when I type lla on the command line, it's actually as if I'm typing/executing the command ls -als. Same for the other two aliases.
As you can see, the longer the real command, the more convenient the corresponding alias is. See the temp alias for an example.
Extending the paths can also be done in a separate file. This way, you're avoiding to clutter the original .bashrc file. An example could be like this:
PATH=/usr/sbin:$PATH
This command will extend the existing path with /usr/sbin. It also adds it in front of the existing path so that files are first searched in the current directory (always like that in Linux) then in the directory /usr/sbin and only if not found, the search will continue in the paths defined in the original PATH variable.
The content of the original PATH variable is retrieved by using the $ token.
Same mechanism/principle can be applied for exports and other stuff you want to "predefine" in files that should be called whenever .bashrc is called.
In the next section there's more detailed information given about the above approach (some repetition of what's mentioned here might be in).
As mentioned in the previous section you can add "extension" to bash. One of them is adding a couple of files that does the following:
Add aliases
Extend/change the default $PATH variable
Add extra EXPORT commands
Add aliasses
To add extra aliases, create a file ~/.bash_aliases. The content of it could be, for example:
alias lla='ls -als'
alias ltr='ls -ltr'
alias grepi='grep -i'
alias gitl="git log --graph --pretty=format:'%Cred%h%Creset%n'"
alias gs='git status'
alias gd='git diff'
alias grepi='grep -i'
alias grepw='grep -w'
Apart from the last two aliases, the other ones are self-explaining.
grepi means search case-insensitive while grepw means search whole words.
Add extra paths
To add extra paths, create a file ~/.bash_paths and define the content like so:
PATH=<paths_you_want_to_add_before_the_official_paths>:$PATH
Example:
PATH=/home/pi/mystuff/libgpiod/gvcresult/bin:/usr/sbin:/home/pi/mystuff/azul_java/java_11/zulu11.60.19-ca-jdk11.0.17-linux_aarch32hf/bin:$PATH
Add extra exports
To add extra exports, create a file ~/.bash_exports and add, for instance, the following content:
export CC=/usr/bin/gcc
From now onwards, each time you refer to ${CC} in makefiles or so the GNU C compiler will be invoked.
Note: you could also shorten the path and just mention export CC=gcc but it's never a bad idea to always give the complete path. There's no ambiguous situation then.
Adapt .bashrc
To have all the above up and running, those files must be called of course. Therefor, the .bashrc file has to be adapted.
Add the following 3 lines to the .bashrc file:
[ -f ~/.bash_aliases ] && . ~/.bash_aliases
[ -f ~/.bash_paths ] && . ~/.bash_paths
[ -f ~/.bash_exports ] && . ~/.bash_exports
Sourcing the file using the command . ~/.bashrc will immediately put those files at work!
Sometimes the colours for certain bash items like the command prompt are not very successfully chosen. Example is the Ubuntu terminal where the path is shown as blue on a black background. That's very hard to read.
To resolve this issue you can change the appearance of those item.
Open the ~/.bashrc file and search for the string PS1.
Example:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w \$\[\033[00m\] '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
Depending on the validity of $color_prompt, the first PS1 or second PS1 will be assigned to the prompt. Mostly, the variable is true, so the first statement will be in effect.
PS1 contains, next to the format, also the colours. To change the path colour go searching for the ':' character on the right of the \u or \h strings. This is highlighted in red in the above example. The colour itself is highlighted in blue in the above example.
By default, the colour used is [01;32] which is the blue foreground colour (see this web page for the different colour options). As said before, this is hardly readable on a black background.
I've changed it into [01;36] which is lightblue on a black background and this is much more readable. See the screenshot below for the result.
If you want to log in into a remote server (e.g. Raspberry Pi running an SSH server) and you want to execute immediately a command you can do so using the -t option of SSH.
The example below does the following:
Moving to a certain directory on the RPi
Activating a Python virtual environment
Starting up a Python application
ssh -X pi@192.168.1.40 -t "cd /home/pi/mystuff/flask/gfg; . venv/bin/activate; python app.py;"
Note that this command also creates an X11 connection to see graphical output on the client, if relevant.
Putting this command on the client machine in a bash script allows you to only run that bash script and it will automatically launch - in this case - a Flask web server. Less typing required from the user, which is good!