Bash and other shells
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.
Before you start
Section titled “Before you start”Finish Practice: Reading and Editing Text (B07). Keep your existing B04 practice directory. Use only ~/lab/b08/shells here.
mkdir -p ~/lab/b08/shellscd ~/lab/b08/shellsThis lesson needs no files. If you repeat it, use Reset this lesson first.
Keep the terminal and shell separate
Section titled “Keep the terminal and shell separate”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?
bash -c 'echo {1..3}'sh -c 'echo {1..3}'Run it, then compare with your guess
bash -c 'echo {1..3}'1 2 3sh -c 'echo {1..3}'{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.
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$ Steps (6)
- Type
bash -c 'echo {1..3}' - Press Enter
- Output:
1 2 3 - Type
sh -c 'echo {1..3}' - Press Enter
- Output:
{1..3}
The static transcript shows both results. Neither result means that the terminal itself understands braces.
Find out what sh means here
Section titled “Find out what sh means here”sh is not a promise that a command starts Bash. On this Ubuntu lab system, follow the link:
readlink -f /bin/sh/usr/bin/dashThe 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.
Some commands run inside Bash
Section titled “Some commands run inside Bash”B04.4 introduced type. Use its answer to connect the command name to the shell:
type cdcd is a shell builtinA 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?
type lsRun it, then compare with your guess
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:
type ifif is a shell keywordA 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.
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$ Steps (9)
- Type
type cd - Press Enter
- Output:
cd is a shell builtin - Type
type ls - Press Enter
- Output:
ls is aliased to `ls --color=auto' - Type
type if - Press Enter
- Output:
if is a shell keyword
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.
Mistakes to recognize
Section titled “Mistakes to recognize”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.
Try it yourself
Section titled “Try it yourself”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
type pwdpwd is a shell builtintype [[[[ is a shell keywordpwd is a builtin; [[ is a Bash keyword. The second answer identifies Bash syntax without teaching its use.
Reset this lesson
Section titled “Reset this lesson”Return to the parent and inspect only this lesson’s directory:
cd ~/lab/b08ls -AR shellsrm -r ~/lab/b08/shellsRerun 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,
shruns Dash, whose expansion rules differ from Bash’s.
Next, Quotes and escaping shows how Bash decides which characters belong to each argument.
Further reading
Section titled “Further reading”- On your system:
man bash,man dash, andhelp type. - GNU Bash: What is a shell?, Invoking Bash, and Aliases.
- Ubuntu 24.04: dash(1).