What is Shell?
If you are working on a Microsoft, Apple, or Linux operating system (OS), you are using shell scripts, perhaps without knowing it. In fact, you interact with shells every single time you boot up your Linux OS.
Shell scripts help programmers, data scientists, and casual users save time and avoid repetitive tasks with automation. For example, scripts can perform daily backups, install patches, monitor systems and activity, and perform routine audits.
Shells read fairly intuitive human-ready commands and turn them into something the system will understand.
What is a Shell?
A shell is a type of computer program called a command-line interpreter that lets Linux and Unix users control their operating systems with command-line interfaces. Shells allow users to communicate efficiently and directly with their operating systems.
Shell scripting is not a single language but, because it uses some natural language commands, it’s easy to learn, even without a programming background. However, each shell scripting dialect is considered a language, and if you plan more complex activities, shells take a lot of practice.
Shell scripting is one of the simplest ways to set up automation. Using Linux or Unix commands, shell scripting gives data scientists, DevOps, and TechOps people conditional and loop control structures to repeat commands.
Shell scripts don’t always go by the same name. Sh,
Bash (the most common), csh,
and tesh are all shell scripts. While in IBM’s VM operating system, they’re called EXEC; in DOS, shell scripts are called batch files.
A shell has two categories, command-line shell and graphical shell. Command-line shells are accessed using command-line interfaces where systems receive input in human-readable commands and then use the command-line interfaces to display the output.
Graphical shells use a graphical user interface (GUI) to perform interactions and essential operations such as opening, closing, and saving files.
Here are some sample shell commands:
To find out what directory you’re in: pwd
To find a command in the manual: man command
To make a text file scrollable: less file 1
or more file 1
Different types of shells
Bourne Shell
As cool as the name may imply, the Bourne shell has nothing to do with spies and high-speed car chases. The Bourne shell, the second most commonly used Unix shell, was created in 1979 by Stephen Bourne at Bell Labs. Like its predecessor, the Thompson shell, Bourne’s executable filename is sh
.
The Bourne shell is the default shell of the Solaris OS. Despite its age, the Bourne shell is popular today because of its speed and compactness. However, the Bourne shell isn’t very interactive and can’t recall its command history.
It also lacks logical and arithmetic expression.
Bourne prompts include:
- Command full-path:
/bin/sh
and/sbin/sh
- Non-root user default:
$
- Root user default:
#
command-line
The C shell, filename csh
, and its predecessor filename tcsh
, is another Unix shell from the late 70s. It was created by Bill Joy, who at the time was a graduate student from the University of California, Berkeley.
Unlike the Bourne shell, the C shell is interactive and recognizes command history
and aliases
. C shells also include C-like expression syntax and built-in arithmetic.
command-line prompts include:
- Command full-path:
/bin/csh
- Non-root user default hostname:
%
- Root user default hostname:
#
KornShell
The KornShell, filename ksh
, was developed in the early 1980s by David Korn at Bell Labs. KornShell includes many of the features of the C shell, and it’s a super-set of, and therefore is backward-compatible, with the Bourne shell.
The KornShell runs faster than the C-shell. It runs Bourne shell scripts and features C-like arrays, functions, and string-manipulation facilities. In addition, it features built-in arithmetic.
KornShell prompts include:
- Command full-path:
/bin/ksh
- Non-root user default: $
- Root user default: #
GNU Bourne-Again shell
The GNU Bourne-Again, or Bash, shell, is an open-source alternative to the Bourne shell. It was designed by Brian Fox for the GNU Project and was released in 1989. Not only is it fully compatible with the Bourne shell, it takes many of the best features from KornShells and C shells. The GNU Bourne-Again shell has automatically mapped arrow keys for editing and command recall.
GNU Bourne-Again prompts include:
- Command full-path:
/bin/bash
- Non-root user default prompt:
bash-x.xx$
- Root user default:
bash-x.xx#
What shell scripting does
This analogy is a bit simplistic, but imagine shell scripts as autofill for programmers. Instead of typing commands into a keyboard one at a time, shell uses sequences of commands in single scripts that users can initiate with a simple filename in the command-line. Shell scripts perform program execution, text wrapping, and file manipulation.
Shell scripting can perform a number of tasks, including monitoring tasks like disk usage, performing backups, creating command tools, manipulating files, running programs, linking programs together, completing batches, etc.
Main alternatives to shell
In IBM’s VM operating system, they’re called EXEC; in DOS, shell scripts are called batch files. Those are still shell scripts, though. Other scripting languages, such as Javascript, Python, Perl, and TCL are also scripting languages. Not surprisingly, computer professionals all have their favorites, and we’ll get to the upsides and downsides to shells in a bit.
Why is Shell so Popular?
Shell is common on nearly every OS because they are efficient and easily updatable. It monitors your computer’s system and performs routine backups without you having to think about it.
There’s no need to switch syntaxes because shell’s syntaxes and commands are identical to those entered in the command-line. In addition, it’s easy and quick to write shell scripts: they start quickly and they’re easy to debug.
What are the Disadvantages to Shell Scripts?
Shell scripts can be error-prone and difficult to diagnose when problems occur. Shells are not designed for complex or large tasks, and they are slow to execute. Additionally, shells don’t offer much data structure, and there can be syntax or design flaws.
When was Shell Created?
The first shell script was created in the early 1960s by MIT computer staff member Louis Pouzin. His first command-line was RUNCOM
, which liberated computer scientists from repetitive tasks such as compiling, updating, renaming, and moving.
While Pouzin wasn’t a computer language expert, or perhaps because he wasn’t, he believed that command-lines could be a language in and of themselves.
By the mid-1960s, Pouzin teamed up with British scientist Christopher Strachey. Strachey designed a macro-generator that served as a base for Pouzin’s command language, which ran on what was at the time the state-of-the-art Multics operating system.
How Shell Got Its Name
Pouzin named his new language “shell” because it is the outermost layer of operating systems.
Shell Features
Shells are popular among programmers because they are concise. That doesn’t mean they’re basic, however. Shells offer several features, including:
Background processing
One of the biggest benefits to shell scripts is that they can run in the background. Depending on the command, shells can run in the foreground or the background. Foreground processes are visible on the screen and can only run sequentially.
Background processes do not appear on the screen and can run non-sequentially. To run a shell script in the background, users simply need to add an ampersand (&) at the end of the script.
Wildcard substitutions
Wildcard substitutions allow systems to process more than one command at a time or to find snippets of phrases from text files. For example, *
tells the system to match any string, even empty strings. ?
matches a single character, [example]
matches any character (“example” is just an example) and [1-9]
(another example) instructs the shell to match characters within the range.
Command aliases
Shell aliases are shortcut commands. Some aliases are a single word, and others are a single letter. To see the list of aliases, users only need to enter ·alias
.
Command history
There are a lot of ways shells save time and effort, but one particularly convenient feature is its command history. Instead of retyping commands, the history
command shows all the commands that were used during that session.
Filename substitution
Filename substitution is also known as “globbing.” When a word contains characters such as ?
, *
, or [
, or begins with ~
, the shell will see the word as a pattern and will be replaced by an alphabetical list of filenames to match the pattern.
Input/output redirection
Input/output (i/o) redirection allows users to swap the standard input (stdin) and standard output (stdout) to be associated with the display screen, keyboard, or a file.
Piping
Shell piping is another kind of redirection that sends the output of a command/process/program to another command/process/program. This lets the commands/processes/programs operate simultaneously and allows for continuous data transfer without having to pass through the display screen or temporary text files.
Shell variable substitution
When the shell encounters an expression containing special characters, it translates the code into something that is more recognizable for users. That process is called variable substitutions or simply variables.
Variables are also used by more seasoned programmers. If, for example, a programmer doesn’t know the actual values before executing a program, they can use the variable as a placeholder before the code is ready for execution.
How Shells Vary
There are two primary sorts of shells, “shell” (sh) and “bash.” Both run within the Unix operating system. Shell is a generic name for scripting in any shell. As the name implies, bash (Bourne Again Shell) is an improved and extended shell.
Bash uses up-gradation for more functionality, supports job controls, is easier to use than sh, and supports command history. Sh does not support command history; its scripting works in any shell and is more portable.
How Shell has Evolved over the Years
Over the years, shells have evolved and stayed more or less the same. The Bourne shell was a big step forward from the original Thompson shell, but many of the original functions remain. Of course, computers and our expectations have all changed.
There are potentially as many shells as there are users; but fundamentally, there are two types of shells – command-lines or Bourne. Anything else is a dialect rather than a different language.
Unix shells
Unix shells are the original and they are still going strong. Unix shells work on Unix and Unix-related operating systems, such as Macs.
command-lines
command-lines (csh) are the most common Unix shells. command-lines excel at interactive work, including aliases, cdpath, job control, path hashing, directory stacks, etc. They also offer editing and history features.
Tenex command-line
The Tenex command-line (tcsh) was developed by Ken Greer from Caregia Melo University. Tenex was considered an improvement over the original command-line version. Unlike the original C, Tenex offers command-line editing and completion, among other features. In 1981, tsch merged with csh.
Korn shells
Korn shells (ksh) are another Unix shell but it’s sort of a compromise between C and Bourne shells, with backward compatibility with the latter. The Korn shell was developed in the early 1980s by David Korn at Bell Labs.
Bourne again shells
The Bourne Again shell (Bash) is an open-source Unix shell that was written in the late 1990s by Brian Fox for the GNU project as an upgrade to the Bourne shell.
Exotic shells
While they’re all built on C or Bourne shells, programmers have designed 100s if not 1,000s of different dialects. Some are useful, and some are simply fun.
Who uses Shell?
Almost anyone who uses a computer benefits from shell, but they are especially beneficial for system administrators, DevOps people, programmers, and data scientists. Shells are used to automate tasks and applications, install packages, backup or restore data, and of course for programming.
Shell Examples
Shells are as basic or as complex as the user desires. A simple bash greeting, for example, reads, echo “hello $USER”
, while complex shell scripts are almost limitless. Here are some basic examples:
Create a file in bash using a suitable editor. Here we use vi editor.
$ vi example.sh
Press the 'i' key to start inserting the code:
#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 10 ];
then
break
fi
((count++))
done
Execute by using
bash example.sh
$vi example2.sh
#!/bin/bash
echo "Enter first number"
read x
echo "Enter second number"
read y
((sum=x+y))
echo "The result of addition = $sum"
bash example2.sh
Careers with Shell
Rather than a career path, think of shell scripting as a useful tool in a data scientist’s arsenal. Data professionals of all kinds need to know multiple languages (or at least they should), and shell scripts help them use the languages more efficiently.
Still, some companies have some roles that specifically require shell scripting. The average salary is around $78,000. That said, shell scripts are a building block to roles like system administrators, which can bring in six-figure salaries.
Conclusion
Shell is like a string that runs through every programming language. Complex commands are boiled down to concise and intuitive shorthands. Understanding shell is an important skill for any data scientist or computer engineer. Learn about shell in DataCamp's Introduction to Shell course.
Shell FAQ's
What is the kernel?
The kernel is the most essential part of the operating system. The kernel is responsible for allocating memory and time to the computer’s programs, and it manages the communications and filestore in response to system calls.
What is the shell?
The shell is the interface between the kernel and the user.
What are the benefits of shell?
We already know that shell is efficient, and it takes care of many of a user’s boring tasks; but beyond that, shells let users customize the scripts to suit their own needs.
What are the downsides to shell?
A single error could prove to be very costly and difficult to identify. In addition, shells are not very portable between operating systems.
What is the difference between Unix and Linux?
Linux is a Unix clone that was developed by Linus Torvalds. Linux is not in and of itself an operating system; it is a kernel. Unix, however, includes the kernel, the shell, and the programs.
What shells are commonly used on Linux and Unix systems?
Bash, or the Bourne Again shell, is the most commonly used shell, but both Linux and Unix also support command-lines, KornShells, and TCSH.
What is shell syntax?
In natural language, syntax refers to how words and phrases are arranged. Shell syntax is very similar; it refers to the order of operations.
What is an interpreter?
An interpreter interprets single statements into machine code.
What is a compiler?
A compiler translates high-level programs into more complex machine language.
How do you execute a shell file?
Set permission with: chmod+x script-name-here.sh
Then, run it with: ./script-name-here.sh
or sh script-name-here.sh.
blog
How DataLab empowers learners’ personal development
Olivia van Aalst
3 min
blog
Cloud Computing and Architecture for Data Scientists
blog
How can I get certified in data science?
blog
What is Data Science? Understanding Data Science from Scratch
DataCamp Team
16 min
blog
How Scottish Power Trains Data Scientists With DataCamp
tutorial