Last modified: June 11, 2024

This article is written in: 🇺🇸

Shells

A Unix shell is an essential tool that provides a command-line interface for users to interact with the operating system. This interaction occurs in a sequence where the shell reads the user's inputs, translates them into system commands, and then communicates these commands to the operating system kernel for execution, which then interacts with the hardware.

The process can be visualized as follows:

+-------------------+       +---------------+       +--------------+
|                   |       |               |       |              |
|  User Input/      |<----->|    Shell      |<----->|   System     |
|  Output Device    |       | (e.g., Bash)  |       | (Kernel/HW)  |
|  (Keyboard/Screen)|       |               |       |              |
|                   |       |               |       |              |
+-------------------+       +---------------+       +--------------+

Among a variety of available shells like bash (Bourne-Again SHell), zsh (Z Shell), ksh (Korn SHell), tcsh (TENEX C Shell), and sh (Bourne SHell), bash is the default shell for most Linux distributions because of its extensive feature set and user-friendly nature.

Examining Available Shells

To discover the shells installed on your system, you can look into the /etc/shells file. This file contains the paths to all the shells installed on your system. Use the cat command to display the file's content:

cat /etc/shells

The output might look something like this:

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

Identifying Your Current Shell

To find out your current active shell, use the following command:

echo "$SHELL"

Alternatively, you can use the ps command to see the process associated with your terminal:

ps -cp "$$" -o command=""

Switching Shells

If you wish to switch to a different shell, like the Z shell (zsh), you can use the chsh (change shell) command with the -s option and the path to the desired shell:

chsh -s /bin/zsh

You will need to enter your password, and then you may need to restart your session for the changes to take effect. Make sure the shell you're switching to is listed in your /etc/shells file, or else the chsh command may not succeed.

Bash Configuration Files

In the bash shell, several configuration files are read when the shell starts, providing a way to control the shell's behavior and set up things like environment variables, functions, and aliases.

Here are the primary configuration files relevant to a bash shell:

Below is a simple example of a ~/.bashrc script:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Bash also provides system-wide configuration files which affect all users:

Terminals

Terminals, often referred to as terminal emulators, are computer programs that provide a graphical user interface for interacting with a shell. They serve as a conduit for entering commands and viewing their output, allowing users to communicate with the operating system.

The terminal's features extend beyond just command input and output, offering users a range of customization options to suit their needs and preferences. These options include:

On many systems, a new terminal window can be opened quickly using the Ctrl + Alt + T keyboard shortcut. This command brings up a new terminal window where you can begin entering commands.

Terminal Shortcut

Various terminal emulators are available, each offering a different set of features and aesthetics. Some of the popular ones include GNOME Terminal, Konsole, xterm, iTerm2, and Hyper. Your choice of terminal can greatly impact your command-line experience, so it's worth trying out a few to see which one you prefer.

Challenges

  1. Find if there are any existing aliases for a command, like cat. Use alias cat to see the aliases for cat.
  2. Display all aliases currently defined in your shell. Simply execute alias without any arguments.
  3. Open ~/.bashrc in a text editor, add a new alias like alias ll='ls -la'. Save the file, reopen your terminal, and verify the new alias. To remove it, delete or comment out the line in ~/.bashrc, then save and restart your terminal.
  4. Use the find command to search your system for files containing 'profile' in their name. Try find / -name '*profile*'.
  5. Create a new user whose default shell is a non-standard program. For example, useradd -s /bin/tar username creates a user with /bin/tar as their shell. Be aware of the implications this may have on user interaction with the system.
  6. Change your default shell using chsh -s /path/to/shell, then open a new terminal session and explore the new environment. Experiment with commands like alias, set, and declare -f to inspect custom variables, aliases, and functions.

Table of Contents

  1. Shells
    1. Examining Available Shells
    2. Identifying Your Current Shell
    3. Switching Shells
  2. Bash Configuration Files
  3. Terminals
  4. Challenges