Skip to content

Bash and other shells

Module
How the Shell Interprets Input
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
On this page

A command in a set of notes prints three numbers in Bash. Someone changes the shell name to sh, and it prints the braces instead. The command did run, but the shell read its input differently.

By the end, you can distinguish the shell from the commands it runs, identify Bash’s builtins and keywords, and explain why choosing another shell can change a result.

Finish Practice: Reading and Editing Text (B07). Keep your existing B04 practice directory. Use only ~/lab/b08/shells here.

Terminal window
mkdir -p ~/lab/b08/shells
cd ~/lab/b08/shells

This lesson needs no files. If you repeat it, use Reset this lesson first.

In B03.2, the terminal accepted your keys and displayed output. Bash was the program reading command lines inside it. An interactive shell repeatedly reads your input, acts on it, and shows a prompt when ready for another command.

The same shell can read commands without a prompt. Here -c gives a shell one command string to read. The single quotes keep that string together for the shell you are already using. B08.2 explains the quoting rules.

Predict first: will changing only bash to sh keep the printed text the same?

Terminal window
bash -c 'echo {1..3}'
sh -c 'echo {1..3}'
Run it, then compare with your guess
Terminal window
bash -c 'echo {1..3}'
Captured output
1 2 3
Terminal window
sh -c 'echo {1..3}'
Captured output
{1..3}

Bash turns the braces into three numbers before echo prints them. In this lab, sh leaves them unchanged. B08.5 explains brace expansion. Here the important difference is which shell reads the string.

One command string, two shells
learner@rootvolt:~/lab/b08/shells$ bash -c 'echo {1..3}'1 2 3learner@rootvolt:~/lab/b08/shells$ sh -c 'echo {1..3}'{1..3}learner@rootvolt:~/lab/b08/shells$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Bash expands the braces; the lab's sh prints them literally.
Steps (6)
  1. Type bash -c 'echo {1..3}'
  2. Press Enter
  3. Output: 1 2 3
  4. Type sh -c 'echo {1..3}'
  5. Press Enter
  6. Output: {1..3}

The static transcript shows both results. Neither result means that the terminal itself understands braces.

sh is not a promise that a command starts Bash. On this Ubuntu lab system, follow the link:

Terminal window
readlink -f /bin/sh
Captured output
/usr/bin/dash

The target is Dash, a different shell. B06.4 introduced symbolic links and readlink -f; this command identifies the file behind this particular name. Other systems can choose a different target for sh.

The brace example is valid input in both shells, but its meaning differs. Other Bash-specific syntax can produce an error in another shell. A successful run therefore does not prove that two shells interpreted the input identically. Use Bash for this course’s Bash examples, rather than replacing its name with sh.

B04.4 introduced type. Use its answer to connect the command name to the shell:

Terminal window
type cd
Captured output
cd is a shell builtin

A builtin runs as part of Bash. cd changes this shell’s working directory. Starting an ordinary external program would create a separate process; changing that process’s directory would not move its parent shell.

An external program lives in a separate executable file. B04.4 identified head that way. Bash must locate that file before starting it; B08.4 explains that search using output captured on the course VM.

Predict first: does Ubuntu’s familiar ls name go directly to a builtin?

Terminal window
type ls
Run it, then compare with your guess
Captured output
ls is aliased to `ls --color=auto'

No. The existing Ubuntu alias replaces the command name with ls --color=auto. It then runs the external ls program, with the color option added. This is the alias you already saw in B04.4; you do not need to define one.

Recognize shell syntax without executing it

Section titled “Recognize shell syntax without executing it”

Some words belong to the shell’s grammar:

Terminal window
type if
Captured output
if is a shell keyword

A keyword is recognized as syntax by the shell. if begins a conditional construct. It is not an executable file or a builtin command that you can run alone. Conditionals belong to later lessons; asking type about the word does not execute one.

Ask Bash about three kinds of name
learner@rootvolt:~/lab/b08/shells$ type cdcd is a shell builtinlearner@rootvolt:~/lab/b08/shells$ type lsls is aliased to `ls --color=auto'learner@rootvolt:~/lab/b08/shells$ type ifif is a shell keywordlearner@rootvolt:~/lab/b08/shells$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. type identifies a builtin, the existing ls alias, and a shell keyword.
Steps (9)
  1. Type type cd
  2. Press Enter
  3. Output: cd is a shell builtin
  4. Type type ls
  5. Press Enter
  6. Output: ls is aliased to `ls --color=auto'
  7. Type type if
  8. Press Enter
  9. Output: if is a shell keyword
Bash reads syntax before choosing a command Simplified model: Bash recognizes syntax, expands an alias when present, then runs a builtin or an external program. Input: syntax first Keywords include if Alias expansion when present ls becomes ls --color=auto Command selection cd: builtin; ls: executable Separate syntax example {1..3}: Bash expands; Dash does not
Bash reads syntax before choosing a command Simplified model: Bash recognizes syntax, expands an alias when present, then runs a builtin or an external program. Input: syntax first Keywords include if Alias expansion when present ls becomes ls --color=auto Command selection cd: builtin; ls: executable Separate syntax example {1..3}: Bash expands; Dash does not
Simplified model. Bash recognizes syntax and expands aliases before selecting a builtin or an external command; brace expansion is a separate shell behavior.

In words: Bash reads shell syntax, replaces an applicable alias, and selects the command to execute. cd runs inside Bash; the ls alias leads to an external program. Brace expansion changes the words first. This model omits functions and other expansion details, rather than claiming that every input follows only these steps.

Treating sh as another spelling of Bash. The captured brace result differs without an error. Check which shell an example requires before changing its invocation.

Typing a keyword alone. type if asks about syntax; it does not teach a complete conditional. Do not copy if alone as a command to try.

Expecting aliases in every shell. The ls alias comes from this interactive Ubuntu account’s startup configuration. Another account or a noninteractive shell may not define it.

Goal: Classify two names without running the syntax they name.

Ask Bash about pwd and [[. State which is a builtin and which is a keyword. You only need type; do not try to construct a conditional.

Hint

Give each name as an argument to type. An argument naming syntax is not the same as starting a command line with that syntax.

Solution
Terminal window
type pwd
Captured output
pwd is a shell builtin
Terminal window
type [[
Captured output
[[ is a shell keyword

pwd is a builtin; [[ is a Bash keyword. The second answer identifies Bash syntax without teaching its use.

Return to the parent and inspect only this lesson’s directory:

Terminal window
cd ~/lab/b08
ls -AR shells
Terminal window
rm -r ~/lab/b08/shells

Rerun the setup under Before you start. If the directory is already absent, skip removal. Close any program using this directory first.

  • A terminal hosts interaction; a shell reads command input.
  • Builtins run inside Bash; external programs live in executable files.
  • An alias can replace a command name before execution; keywords belong to shell grammar.
  • On this Ubuntu lab system, sh runs Dash, whose expansion rules differ from Bash’s.

Next, Quotes and escaping shows how Bash decides which characters belong to each argument.