When working on Linux, users or administrators has to manage files and filesystem, run programs, or execute scripts. The system provides a command line interface to perform these tasks.
In this tutorial, let's learn about the shell which is an important part of the Linux operating system.
What is Linux Shell means?
The shell in the Linux operating system is used as a command line interpreter. It works between user input and Linux Kernel. The user’s requests are taken in the form of commands via the terminal that is passed to the Linux default shell, which tells the Linux Kernel to perform the specific function. Then the Kernel processes the hardware resources to show the output on the terminal.
The shell uses the system resources, including CPU and memory, to execute the commands. But very minimal resource usage when compared to a graphical one.
On Ubuntu, to start a shell simply open gnome-terminal and type the commands at the prompt. This will start a bash session to work on.
The Linux shell has some of the following advantages over the Graphical user interface:
- Easily managing files and directories
- Efficient and powerful when working with large amounts of data or when automating tasks.
- Command history.
- Tab completion.
- Command Aliases.
- Perform networking tasks such as ssh, file transfer, etc.
- System administration.
Common Linux Shells
The very first shell was created in 1979 by Stephen Bourne and named it sh (Bourne shell). It was the widely used shell for Unix and other systems. There are several different types of shells available for Linux. All those evolved over the years with many versions and variations.
Some of the commonly used Linux shells are:
- Bash (Bourne Again Shell) - Developed by GNU, first released in 1989. It's the most used shell on Linux due to its flexibility and compatibility with other Unix-like systems.
- Ksh (Korn Shell) - It is a high-level programming language developed to improve the Bourne shell.
- Csh (C Shell) - This shell was developed to include arithmetic operations and other functions missing in the sh shell. The C programming language functions were also introduced with the ability to save the command history in the C shell.
- Zsh (Z Shell) - The Z shell is based on the Bourne shell with several improvements and advanced functionalities. The Z shell is the most advanced Linux shell that supports command completion, themes, and plugin support.
- FISH ((Friendly Interactive Shell) - A user-friendly interactive command-line shell developed to improve usability and interactivity in different Linux distributions. It provides advanced features such as tab completion, highlighting the syntax, and suggesting commands based on user history.
- Ash (Almquist Shell) - It is a POSIX-complaint lightweight Unix-based shell that replaced the Bourne shell. This shell has fewer libraries than the bash shell and executes the programs quickly. This shell is suitable for previous computers and users having less memory.
Most Linux distributions keep bash as the default shell, which makes many users stick to it. But if you prefer specific features or functionality you choose modern shells such as Zsh or Fish.
Related Read: 4 Ways to Check Which Shell You are Using on Linux
The shell is operated through the command line shell prompt, where the user inputs the commands. The prompt contains the character or series of characters. It is usually displayed as username@hostname followed $ and a space.
The user input is written in front of the $ sign. The prompt takes the input as commands and executes those commands by using the system resources to perform a specific task. The command is written on the shell prompt, and press Enter key to run the program.
To login to the shell prompt as the root user (administrative user), use the below su (switch user) command:
$ sudo su -
The output shows that the login user is switched to the root user and the dollar sign is changed to # to symbolize its ready execution with admin privilege.
Moreover, the shell prompt look can be customized by utilizing its shell configuration file. Every shell has its configuration file, such as the bash shell with .bashrc and the C shell with .cshrc configuration file.
Related Read: Customizing Your Bash Prompt in Linux: Changing Colors
Common Commands of Linux Shell
Commands which are entered on the shell prompts are called shell commands. To run a command simply type the command and press Enter key. Linux operating system by default comes with lots of builtin-in shell commands.
Shell commands can do various tasks such as content listing, showing disk usage, content copy/move/deletion, file operations, and more.
The common shell commands supported by every Linux Distros are:
- ls - To list the content of a folder.
- cd - It changes the current directory to the destination directory.
- pwd - It shows the user's current working directory.
- cp - It copies the directory/file to another directory/file.
- mv - It moves the directory/file to the destination directory/file and is also used to rename the directory.
- mkdir - It creates an empty directory.
- rm -r - It removes the file or directory with all its sub-directories.
- clear - It clears the terminal screen.
For example to show the current working directory and lists its files and directories, type the following commands from the shell prompt:
Here we have used pwd and ls shell commands.
Navigate all-in-one place of Linux Commands for more learning.
Writing Shell Scripts
We learned about different shells in Linux. It's possible to write scripts in different shells. A shell script is basically a script written for the shell to execute. A script generally includes multiple commands or statements which execute line by line. The statements include conditions, loops, and functions used within the shell scripts to perform complex tasks easily.
Most Linux distributions by default come with a bash shell. Bash scripts are interpreted by the bash shell, so they can be run on any platform that has bash installed. Bash is one of the popular shell among Linux users. Because of that the terms 'shell script' and 'bash script' are often used interchangeably.
For this guide, we are using Ubuntu, which comes with the default Bash shell. Let's check the basic syntax of a bash script:
#!/bin/bash #Enter commands or conditions statements in this section <Commands or statements>
- #!/bin/bash - It represents the start of the bash shell script. To use the shell script, the “bash” will be replaced with desired shell extension.
- Commands/statements - In this section add the commands (echo, ls, etc) or statements (read, if, for, while, functions etc).
- # - It shows the helping comments within the bash scripts which are not executed.
The following script is named examplescript.sh which prints the username of the current user and lists all files/directories in the current directory.
$ cat examplescript.sh
#!/bin/bash #Prints the username whoami #Lists the file & directories ls
To execute the bash shell scripts, first, we must provide the execute permissions to that file:
chmod +x examplescript.sh
The chmod command provides the execute permissions to the examplescript.sh file can be verified using the ls -l command. The output verifies that the execute permissions are provided to the file.
You may use any of the following methods to run the bash script:
The above output shows the execution result of the examplescript.sh script.
Understanding Shell Variable
A shell variable is a named value that can be assigned and used by shell and also by other programs. There are two types of variables: local variables and environment variables.
- Local variables - defined and used within the context of a single shell or script.
- Environment variables - defined and exported by the shell, and they are available to all child processes.
You may use environment variables when multiple programs or scripts require it and whereas local variables intermediate values within a single shell or script.
You can simply create and assign a value by name=value. Example
On the other hand to set environment variables use the export command. Example:
To display the value of the environment variable or local variable use echo command followed by variable_name. Example:
To display local variables including other variables use the set command and to display the environment variables in Linux, you can use the env or printenv command.
Here we learned Linux operating system comes with a shell. There are many different shells available for Linux. If you prefer you may add more shells and use multiple shells on the same system.