Skip to main content

Shell and bash

Shell and bash

You hear the term shell, shell script etc. quite frequently in ubuntu world. So what is a shell?

Shell is a command interpreter. It accepts commands from user and interacts with the kernel of the operating system and gives you the output. 

A shell can be either a text shell or a graphical shell. All of us use the graphical shell - which lets us create, copy, delete files and many other operations. A graphical shell is easier to use.

A text shell or a CLI (command line interface) is a text based command interpreter. When we talk about shell, normally we mean this text based CLI.

Linux systems have different shells available for the users. Bourne shell, C shell, bash etc. are the more common ones. 

bash - (bourne again shell) is a shell we use very often in Ubuntu systems.

To know available shells in your system, just cat /etc/shells

We have earlier seen that to start a terminal or a shell we need to press CTRL+ALT+T command. This opens a new terminal and starts a new session of bash. 

Is there any other way of starting a terminal?

Login shells and non-login shells

When we start a new terminal, the shell does not ask us our user name and password. It takes the login information from GUI interface. 

But if we combinations CTR+ALT+F1 through CTRL+ALT+F6, then a new tty(teletypewriter - a historical name) terminal is opened which asks for login credentials to start with - user name and password. 

To come back to our graphical terminal, we need to press CTRL+ALT+F7 

This is a login shell. Which means we can simultaneously open 6 login shells along with our GUI terminal.

Bash startup files 

Bash and also other shells use startup files to set environment variables. These files are ~/.profile, /etc/profile,~/.bashrc,/etc/bash.bashrc 

.profile in user's home directory and /etc/profile in /etc directory are executed when a login shells are started (that is tty terminals)

But for non-login shells - shells started by opening a terminal in GUI screen, the existing environment variables are copied and profile files are not executed.

But for these non-login shells .bashrc in home directory and /etc/bash.bashrc are executed.

Let us have a look at typical .profile file.
And if we are using a login shell, then on logout, the script file bash_logout is executed.

If there is a file .bash_profile in home directory or /etc directory, then that is executed on startup instead of .profile file.

exit command

exit command terminates a shell either in login or non-login shells.

Repeating commands and command completion

bash has two beautiful features which help a lazy user. 

The commands you have typed in history file and by using up arrow repeatedly you can repeat earlier commands. Up arrow once shows previous command, twice shows the command before that and so on.Quite helpful.


If you type partial command and press tab key, bash gives command completion. If there are multiple options, it shows the options to you. 

Let me repeat some thing again. If you don't know a command, just ask your shell
whatis comd

And shell gives you one line info.
 

Comments