Like a Boss

This module doesn’t have new commands, it’s to help you use the shell faster and more efficiently.

How Commands are Processed (Again)

Remember that commands are executed in stages. Understanding the stages is important to understanding error messages that appear when you make a mistake typing a command. Today we’re going to learn about the parse stage where the command is interpreted and characters like *, quotes and $ get their special meaning.

Step Title Description
1 Prompt Print the prompt and wait for the user to type a command and the Enter key
2 Parse Parsing is the process of turing the command you typed to a plan for action. During the parsing phase the shell breaks the line into a command and arguments. The shell also processes special characters like the wildcard * character and shell variables.
3 Search Once the command name is determined the shell searches for the command.
4 Execute When the command is found the shell exectues the command.
5 Nap While the command is running the shell goes to sleep and waits for the command to finish.
6 Repeat Go back to step 1!

About Parsing

A parser is a computer program that makes sense of input code. It’s important to understand that the parser is the first step that the shell takes after you press Enter, before the command is located and executed. When you run the command below:

$ cat *.txt 

The shell is the program that resolves the files that match *.txt. It replaces *.txt on the command line with the results of the match. The cat program is none the wiser. Try this and you’ll see what happens when you give cat a literal *:

$ cat '*.txt'

Globbing

Globbing is the UNIX term for using wildcards to tell the shell what files or directories to put on the command line. This saves you a ton of typing. Here’s an example of the most common forms of globbing.

Tab Completion

The Tab key tells the shell to do some tedious work for you. It tells the shell to guess what you’re going to say next. The shell has a function called Tab completion than enables guessing. Here’s an example of how tab completion saves time constructing a long command:

Quoting

Sometimes you need quotes to escape the special meaning of a character such as $ or *.

Command Substitution

What if you wanted to run a command, take its output and copy-and-paste it onto the command line? For example, what if you wanted to check the permissions on everybody’s home directory? You can see a list of home directories using cat and cut:

$ cat /etc/passwd | cut -f6 -d: 

But now you have to copy and paste those onto the command line. How can we make that less tedious? We can put the command on the command line.

$ ls -ld $(cat /etc/passwd | cut -f6 -d: )

In the demonstration notice that I use the Ctrl-a and Ctrl-e keyboard shortcuts to get to the beginning and end of the line!