Linux › Section B · Basic commands and the directory hierarchy › Topic B.1

The Bourne shell and using it

The shell is the program you talk to when you type commands, and much of Linux, from start-up scripts to administration, is built on it. This topic introduces the Bourne shell family, the terminal window and the shape of a command, using cat to see how programs read and write.

What a shell does

The shell is your command interpreter. It shows a prompt, reads the line you type, works out what you meant and then either runs another program or carries out the command itself, since some commands are built into the shell. It is also a small programming language. A shell script is a text file containing a sequence of shell commands, and many parts of a Linux system, from installers to start-up tasks, are shell scripts; the section "Shell scripts" teaches you to write them. What you typed stays on screen, so a mistake is easy to spot and retry.

From the Bourne shell to bash

Most Unix shells in common use descend from the Bourne shell, sh, written by Stephen Bourne at Bell Labs and released with Version 7 Unix in 1979; the C shell, csh, is a separate line. Its behaviour was later standardised by POSIX, and in practice every Unix-like system provides a POSIX-compatible shell at /bin/sh, because many system scripts expect one. Linux's usual interactive shell is bash, the GNU Bourne-Again Shell, first released in 1989. What /bin/sh points to varies: on Fedora and RHEL it is bash, while Debian and Ubuntu use dash, a small, fast shell for scripts, and keep bash for logins. Other shells, such as zsh, the default on macOS since 2019, add features but keep the Bourne style. This course uses bash.

Terminal and shell are different things

Two programs cooperate when you work at a command line. The terminal, or terminal emulator, is the window: it draws characters, takes your key presses and scrolls the output. The shell is the program running inside it that interprets what you type. On a desktop such as GNOME or KDE you open a terminal application, which starts your shell in a new window. A text console without graphics, or a remote login over SSH, gives you a shell without a desktop window at all. Fonts and colours belong to the terminal; commands and variables belong to the shell.

Reading the prompt

Once the shell is ready it prints a prompt. Ubuntu's default looks like ana@laptop:~$ and Fedora's like [ana@laptop ~]$: your username, the machine's name, your current directory, where ~ means your home directory, and a final $. When a shell is running as the superuser, the prompt ends in # instead, a warning that every command has full power. In this course, a command shown after $ is typed without the $ itself, and lines without a prompt are output. Try it: type echo Hello there and press Enter. The shell runs echo, which prints Hello there and then the prompt returns.

Anatomy of a command

A command usually starts with the name of a program to run, followed by arguments, separated by spaces, that tell it what to work on and how. Arguments that change the program's behaviour are options. Short options are one dash and a letter, such as -l, and most programs let you combine them, so ls -la means ls -l -a. Long options use two dashes and a word, such as --all, which for ls means the same as -a. The remaining arguments are usually file or directory names. Because spaces separate arguments, a name containing a space must be quoted, as in cat 'my notes.txt'. Shell built-ins and some special forms bend these rules, as later topics show.

cat: joining and printing files

The cat program is about as simple as Unix commands get: it reads each file named as an argument, in order, and writes the contents out, then exits. Its name comes from concatenate, because given several files it prints them one after another as a single stream. With one file it simply displays it, which is how it is most often used: cat /etc/hostname shows the machine's name. Options adjust the output; for example, -n, or --number, numbers every output line. For long files a pager is better, as the topic "Intermediate commands: grep, less, diff, find, sort" explains.

Worked example: taking a command apart

A directory holds notes.txt, containing one line, Linux is a kernel., and todo.txt, containing buy milk and call Sam. Run cat -n notes.txt todo.txt. 1. The shell splits the line at spaces into four words: cat, -n, notes.txt and todo.txt. 2. The first word names the program; -n is an option; the last two are file arguments. 3. cat reads notes.txt and then todo.txt as one stream. 4. Run in bash 5.2, the output was three numbered lines: 1 Linux is a kernel., 2 buy milk, 3 call Sam. Check: numbering ran on across both files, which shows cat treated them as one stream rather than restarting at 1.

Standard input and standard output

Programs read and write through streams that the kernel sets up for every process. Run cat with no file arguments and it seems to hang: no prompt returns. It is reading its standard input (stdin), which here is connected to your terminal. Type a line and press Enter, and cat repeats it, because it writes to its standard output (stdout), also connected to the terminal. Press Ctrl+D at the start of an empty line to send end-of-file (EOF); cat finishes and the prompt returns. Ctrl+C is different: it interrupts a program whatever it is doing. A third stream, standard error, carries messages, as "Shell input and output: redirection and pipes" shows.

Worked example: which shell is which?

You want to know what shell you are using on an Ubuntu server. 1. Run echo $SHELL. It prints your login shell, for example /bin/bash, recorded for your account. 2. Run bash --version. On the test machine for this course the first line read GNU bash, version 5.2.37(1)-release; yours will name its own version and platform. 3. Run readlink -f /bin/sh to see what /bin/sh really is. Illustrative output on Ubuntu: /usr/bin/dash. On Fedora it would be /usr/bin/bash. 4. So interactive work uses bash, but scripts starting with #!/bin/sh run under dash on Ubuntu. Check: a script using bash-only features may work for you yet fail as an sh script there.

Common mistakes to avoid

First, typing the $ of the prompt as part of a command. Second, confusing the terminal window with the shell running inside it. Third, assuming /bin/sh is always bash; on Debian and Ubuntu it is dash. Fourth, writing ls - la with a space, which passes a lone - and a separate la. Fifth, forgetting to quote a filename that contains spaces. Sixth, pressing Ctrl+C when you meant to end input with Ctrl+D, or thinking cat with no arguments has frozen when it is waiting for input. Seventh, running everyday commands in a root shell whose prompt ends in #.

Key takeaways

A shell reads commands, runs programs or built-ins, and doubles as a scripting language. Bourne-style shells descend from sh (1979); bash (1989) is Linux's usual shell, and /bin/sh is bash on Fedora and RHEL but dash on Debian and Ubuntu. The terminal is the window; the shell runs inside it. Prompts end in $ for ordinary users and # for root. A command is a program name plus arguments, with short options (-l, combinable as -la) and long options (--all). cat prints and joins files; with no arguments it reads stdin until Ctrl+D, while Ctrl+C interrupts.

Flashcards

Flashcard

Shell

Program that reads command lines and runs programs or built-ins

Also a scripting language.

Test your understanding

Question 1 of 5

You run cat with no arguments, type two lines and see each one repeated. How do you end it normally and get the prompt back?

Question 2 of 5

Which command means exactly the same as ls -l -a?

Question 3 of 5

A script begins with #!/bin/sh and uses bash-only features. It works on Fedora but fails on Ubuntu. What is the most likely reason?

Question 4 of 5

notes.txt has 2 lines and todo.txt has 3. What does cat -n notes.txt todo.txt show for the last line?

Question 5 of 5

Your prompt changes from ana@laptop:~$ to root@laptop:~#. What does the # tell you?

← Previous topicNext topic →