Bash Shell
Basic informations
Types
There exist multiple different types of shells in Linux :
- Bourne Shell (sh)
- C Shell (csh or tcsh)
- Korn Shell (ksh)
- Z Shell (zsh)
- Bourne Again Shell (bash)
All of them serve a common purpose, which is to facilitate communication between the user and the operating system.
To check the shell being used, we have to use the following command:
marijan$ echo $SHELL /bin/bash
Bash is probably the most popular shell because of these features.
It's possible to change the default shell using the following command :
marijan$ chsh Enter the new value, or press enter for the default Login Shell [/bin/bash]: /bin/sh
It's important to specify /bin/ before the type of shell you want to use.
Features
Bash has multiple interesting features that make him the most used type of shell. Here is some examples :
- Auto-Completion : When you are typing a command, you don't really need to type in the entire command. Indeed, you can type the beginning of the command or the name of your file or folder, and then press tab to auto-complete. Here is a example with the command ls :
marijan$ ls Docu [PRESS TAB] Documents
- Alias : You can create an alias if you would like to save time or if it's easier for you to remember some commands. For instance, here we have created an alias for the date command :
marijan$ alias dt=date marijan$ dt Thu 21 Mar 2024 04:09:44 PM CET
- History : We can view the history of commands by using the following command :
marijan$ History ls Documents alias dt=date dt
Variables
Variables are used to store values and to print it in the shell, you have to use the dollar ($) symbol.
You can create your own variable and set it to a sentence :
marijan$ var1='Hello my name is Marijan' marijan$ echo $var1 Hello my name is Marijan
If you would like to include special characters, you have to use double quotes :
marijan$ var2="H3ll0 H0w @re y0_u ?" marijan$ echo $var2 H3ll0 H0w @re y0_u ?
To include your variable in a sentence in your shell, you have to enclose the variable in brackets :
marijan$ Here is a big sentence ${var2}¨ Here is a big sentence H3ll0 H0w @re y0_u ?
Define a variable as shown here apply only to the current shell session. After a restart, the value will be deleted. If you want to make them persistent, you have to add them to the file ~/.profile or ~/.pam_environment.
Environment
Environment variables are used to store information about the user's login session. To see a list of all of these variables, we can use the following command :
marijan$ env SHELL=/bin/bash LANGUAGE=en_US:en PWD=/root LOGNAME=root XDG_SESSION_TYPE=tty MOTD_SHOWN=pam HOME=/root LANG=en_US.UTF-8 SSH_CONNECTION=212.147.110.1 51872 172.16.0.15 22 XDG_SESSION_CLASS=user TERM=xterm USER=marijan SHLVL=1 XDG_SESSION_ID=17 XDG_RUNTIME_DIR=/run/user/0 SSH_CLIENT=212.147.110.1 51872 22 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin SSH_TTY=/dev/pts/0 _=/usr/bin/env
To set an environment variable, you have to use the export command and usually, all of the environment variables are in uppercase :
marijan$ export MARIJAN=msa
To change the information displayed on the prompt, we have to modify the value of the default variable PS1. Indeed, there exist several options, for instance :
\d: Date of the day ;
\h: Name of the machine ;
\H: Full name of the machine (with the domain) ;
\t: Current time ;
\u: Name of the user ;
\w: The current working directory ;
\W: The basename of the current directory. ;
By default, PS1 shows only \u (name of the user) and \h (name of the machine) :
marijan@debian-vm:~$
It's also possible to change this information by altering the prompt string directly, without using specific options. For example :
marijan@debian-vm:~$ PS1="debian-server" debian-server:~$
By the way, [~] represents the present working directory and $ is the user prompt symbol.
Scripting
To save time and increase productivity by setting up a solution for automating repetitive or complex tasks on a Linux system, you can use Shell Scripts. Here is a list of a few tasks you can accomplish with them:
- Automating Daily Backups ;
- Automating Installations ;
- Periodically Monitoring the System ;
- Raising Alarms and Sending Notifications ;
If you want to set up scripts with Shell, you have to know the basics of Linux.