# The Foundations

# Why use the command line?

  • Because it gives you flexibility and mobility
  • Because it lets you interact with your system in a faster, more concise way
  • Because some functionality can not be accessed through the GUI

# Shell

The shell is an interpreter that translates user commands into actions performed by the operating system. Current shells include a lot of features the most notable of which are:

  • Scripting - The ability to place commands in a file and execute them from there
  • Aliases - The ability to create 'nicknames' for long commands
  • Variables - Store and reuse information

# Command Format

Commands follow a strict layout

> COMMAND [SUB-COMMAND] [OPTIONS] [ARGUMENTS]
1

most commands can be run without arguments. If arguments are required this will usually cause them to display a help text and exit.
Keep in mind that Linux is a case sensitive system.

# History

Your shell will keep a record of the commands you executed. By default this record is persisted between sessions in most shells. The number of commands stored can be configured. Lets give it a try.

  • Display the current date
 > date
1
  • Display the calender for March of 2019
> cal 3 2019
1
  • Display your command history
> history
1

# Advances History concepts

Command Meaning
history 5 Show the last 5 commands
!! Recall last command
!-5 Recall command 5 from the bottom of the history
!4 Recall command 4 from the history

# Variables

The echo command allows you to display a line of text. It will resolve variables. We can therefore use it to display their values.

The variable HISTSIZE holds the maximum length of our history.

> echo $HISTSIZE
1

will display that value. $ tells the shell that the following value is a variable and should be resolved.

To set a variable you can simply assign a value using the = operator

> HISTSIZE=500
1

Notice that there are no spaces between the = and the values. We will talk about how to persist assignments and variable scope later.

# The Path

$PATH is one of the most important variables in your shell. It tells your shell where to look for commands.

TIP

Display your PATH now.

Lets say you have a command zed located at /usr/bin/custom/path/zed

Trying to run

> zed
1

will result in command not found

If you want to call custom commands/software you need to add the location of that software to the PATH.

> PATH=/usr/bin/custom/path:$PATH
1

# Exporting Variables

Shells typically know two types of variables local and environment. By convention local variables are lowercase while environment variables are all-caps. Creation of variables always happens via assignment (=) and variables you create are by default local.
To create an environment variable you can use the export command.

> var1=Something
> echo $var1
1
2

Look in the environment for this variable by using env which displays the environment and grep a pattern matching tool.

> env|grep var1
1

You will not find var1. The above command chain uses the pipe operator | to combine the two commands. This is a very powerful feature we will explore later.

After running

> export var1
1

the same search shows that var1 is now part of the environment.

TIP

Now lets try these things:

  • Export the word Else as local variable var2
  • Assign the combined value of var1 and var2 to the environment variable var1.
    Hint: You can "escape" spaces using single quotes.
  • Use unset to remove var2. What happens to var1?
  • Remove var1 from the environment

# Information on Commands

# which

Sometimes multiple versions of a command are installed on a system. To determine which one is called just use which. For example

> which date
1

which uses the PATH to determine which command is actually called.

# type

The type command can be used to infer more details about a given command. In its base call it describes the command by classifying it. This can look very similar to the output of which. Using the -a flag will yield more details.

TIP

Go and try type -a on echo.

type can also be used to identify aliases and resolve functions. Try to resolve what ls stands for in your shell.

# Excursus: Aliases

Aliases are used to shorten longer commands. On execution they are replaced by their longer counterparts. A common example is ls -l which is commonly shortened to ll.

You can use alias to display your current aliases. To create an alias use

> alias lh='ls -Shl'
1

be aware that aliases created this way are only persisted in your current shell until it is closed. For more persistent aliases refer to your shells documentation.

# Globbing

Glob characters are often referred to as wildcards. They are symbols with special meaning in the shell. Unlike commands they are interpreted by the shell itself and can be used with any command. This allows manipulation of multiple files or directories at once.

Character Meaning Example
Asterisk (*) Zero or more occurrences of any character echo /etc/*.d
Question Mark (?) Any one character echo /etc/*.???
Square Brackets ([]) Match a single character contained in the bracket. See below
Exclamation Point (!) Negate the match ![DP]*

# Square Brackets

> echo /etc/[gu]*
1

will match any file starting with g or u followed by any number of characters

> echo /etc/[a-d]*
1

will match any file beginning with a letter between a and d

> echo /etc/*[0-9]*
1

will list any file that contains at least one number

> echo /etc/*[9-0]*
1

will not match anything because the range depends on ASCII tables. This means that ordering of elements is relevant.

TIP

Try running the above commands to get a feeling for how globbing works.

# Quoting

The shell knows three types of quotes single-quotes ', double-quotes " and back-quotes often referred to back-ticks `.

# Double Quotes

Double quotes will stop the shell from interpreting some meta-characters like glob characters but does allow things like command substitution, variable substitution and some not yet discussed shell meta characters.

TIP

Try echoing the $PATH and prefixing it with The path is:

# Single Quotes

Single quotes will prevent the shell from performing any kind of interpretation on the string contained within.

# Backslash

You can use the backslash character \ to essentially single quote a single character.

TIP

Try echoing

You have $100 and the path is: $PATH

While displaying the money value correctly and resolving the $PATH variable.

# Back Quotes

Back quotes are used in a process called command substitution. In essence they tell the shell to execute the command enclosed by them and substitute the output of this execution in the result.

TIP

Try echoing the sentence

Today is date

and replacing date with the result of the date command.

# Control Statements

Can be used to chain commands or for conditional execution.

# Semicolon

The semicolon ; is used to chain commands together.

TIP

Using a single line input try displaying the calender for the first three month of 2019.

# Double Ampersand &&

Used to conditionally execute a command on successful completion of the command before. Acts like a logical "and".

TIP

Try echoing success after a successful directory listing.

# Double Pipe ||

Used to conditionally execute a command on failure of the previous command. Has an "Either run the first command successfully or run the second command" semantic.

TIP

Try echoing failure after a failed directory listing.