If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Today, I’ll be covering the Linux “Shell”. In addition, I’ll be touching on the commands, who and type but the focus of this article is learning how the shell works, and by extension, what you are actually doing when you enter commands.
In order to run commands on a Linux system, you’ll need somewhere to type them. That “somewhere” is the shell, which is Linux’s command-line user interface: you type a command and press Enter, and the shell runs whatever program (or programs) you’ve requested. There are different ways of running the shell, depending on your Desktop Environment (if any):
- Gnome: Menu->System Tools->Terminal (Or right-click the desktop, choose “Open Terminal”). This will launch gnome-terminal
- KDE: Menu->System Tools->Terminal (Or right-click the desktop, choose “Open Terminal”). This will launch konsole.
- twm: Right click the desktop, choose “Xterm”. This will launch xterm.
- No Window Manager: If you have no window manager installed, the shell is what you’re looking at immediately after logging in. This is handy to keep in mind if you ever get locked out of X11 (after installing new video drivers, for instance)
For example, if you want to see who’s logged in, you could execute this command in a shell:
$ who
george :0 Sep 23 20:44
john pts/0 Sep 15 13:51
mary pts/1 Sep 22 21:15
mary pts/2 Sep 22 21:18
(The dollar sign is the shell prompt, which means the shell is ready to run a command.) A single command can also invoke several programs at the same time, and even connect programs together so they interact. Here’s a command that redirects the output of the who program to become the input of the wc program, which counts the lines of text in a file; the result is the number of lines in the output of who:
$ who | wc -l
4
telling you how many users are logged in (actually, how many interactive shells those users are running. If a user has several shells running, like the user mary in my example, they’ll have that many lines of output in who). The vertical bar, called a pipe, makes the connection between who and wc.
A shell is actually a program itself, and Linux has several. I’ll focus on Bash (the “Bourne-Again Shell”), located in /bin/bash which is the default in Ubuntu.
The Shell Versus Programs
When you run a command, it might invoke a Linux program (like who), or instead it might be a built-in command, a feature of the shell itself. You can tell the difference with the type command:
$ type who
who is /usr/bin/who
$type cd
cd is shell builtin
It is helpful to know what the shell provides versus what Linux does. The next few sections describe features of the shell.
Selected bash Features
A shell does much more than simply run commands. It also provides powerful features to make this task easier. Examples are wildcards for matching filenames, redirection of command output and input to and from files, pipes for making the output of one command become the input of another, aliases to run common commands quickly, variables for storing values for use by the shell, and more. Run info bash for full documentation.
Wildcards
Wildcards provide a shorthand for specifying sets of files with similar names. For example, a* means all files whose names begin with a lowercase “a”. Wildcards are “expanded” by the shell into the actual set of filenames they match. So if you type:
$ ls a*
the shell first expands a* into the filenames that begin with “a” in your current directory, as if you had typed:
$ ls aardvark adamantium applels never knows you used a wildcard: it sees only the final list of filenames after the shell expansion.
| Wildcard | Meaning |
| * | Any set of characters except a leading period |
|
? |
Any single character |
| [set] | Any single character in the given set, most commonly a sequence of characters, like [aeiouAEIOU] for all vowels, or a range with a dash, like [A-Z] for all capital letters. |
|
[^set] |
Any single character not in the given set (as above) |
When using sets, if you want to include a literal dash in the set, put it first or last. To include a literal closing square bracket in the set, put it first. To include a ^ or ! literally, don’t put it first.
Brace expansion
Similar to wildcards, expressions with curly braces also expand to become multiple arguments to a command. The comma-seperate expression:
{a,b,cc,ddd}expands to:
a b cc dddBraces work with any strings, unlike wildcards which are limited to filenames. For example, sand{X,Y,ZZZ}wich expands to:
$ echo sand{X,Y,ZZZ}wich
sandXwich sandYwich sandZZZwich
regardless of what files are in the current directory.
Tilde expansion
The shell treats tildes (~) as special characters if they appear alone or at the beginning of a word.
~: Your home directory
~george: User george’s home directory
That covers today’s installment. Tomorrow, we will begin part 2 of the Shell, and cover shell variables, search paths, and aliases.
Related posts:




