Table of Contents Link to heading
- Command-Line Interface
- The Shell
- Commands
- Options
- Arguments
- Command History
- Variables
- Types of Commands
- Aliases
- Functions
- Quoting
- Control Statements
- Some Useful Commands
Command-Line Interface Link to heading
- The Linux community promotes the CLI due to its power, speed and ability to accomplish a vast array of tasks with a single command-line instruction.
- The CLI provides more precise control, greater speed and the ability to automate tasks more easily through scripting.
- By learning the CLI, a user can easily be productive almost instantly on any flavour or distribution of Linux.
The Shell Link to heading
- Once a user has entered a command, the terminal then accepts what the user has typed and passes to a shell.
- The shell is the command-line interpreter that translates commands entered by a user into actions to be performed by the operating system.
- The Linux environment allows the use of many different shells.
- The most commonly used shell for Linux distributions is called the Bash shell.
- The Bash shell also has many popular features, a few of which are:
- Command-line history
- Inline editing
- Scripting
- The ability to place commands in a file and then interpret (effectively use Bash to execute the contents of) the file, resulting in all of the commands being executed.
- Aliases
- The ability to create a shortcut that references a command.
- Variables
- Used to store information for the Bash shell and for the user.
- When a terminal application is run, a shell appears displaying an important
part of the interface — the prompt.
- Typically the prompt contains information about the user and the system.
- The prompt shown contains the following information:
- Username (sysadmin)
- System name (localhost)
- Current Directory (~) - The ~ symbol is used as shorthand for the user’s home directory.
Commands Link to heading
a software program that, when executed on the CLI, performs an action on the computer.
- To execute a command, the first step is to type the name of the command.
- If you type
ls
and hit Enter. The result should resemble the example below:
- Some commands require additional input to run correctly.
- This additional input comes in two forms: options and arguments.
- Options are used to modify the core behaviour of a command.
- Arguments or parameters are used to provide additional information (such as a filename or a username).
- The typical format for a command is:
command [options] [arguments]
Options Link to heading
can be used with commands to expand or modify the way a command behaves.
- For example, using the -l option to the ls command results in a long listing, providing additional information about the files that are listed
- Often the character is chosen to be mnemonic for its purpose, like choosing the letter l for long or r for reverse.
- Options can be used in conjunction with other options:
- Options are often single letters; however, sometimes they are words or phrases as well.
- Typically, older commands use single letters while newer commands use complete
words for options.
- Single-letter options are preceded by a single dash - character (e.g. -h).
- Full-word options are preceded by two dash – characters (e.g. the full-word form of the -h option, the –human-readable option).
Arguments Link to heading
can be used to specify something for the command to act upon.
If the ls
command is given the name of a directory as an argument, it lists
the contents of that directory.
Some commands (such as ls
) accept multiple arguments:
Command History Link to heading
- When a command is executed in the terminal, it is stored in a history list.
- This makes it easy to execute the same command late by eliminating the need to retype the entire command.
- Pressing the Up Arrow ↑ key displays the previous command on the prompt line.
- To view the entire history list of a terminal, use the
history
command:
- If the desired command is in the list that the
history
command generates, it can be executed by typing an exclamation point ! character and then the number next to the command (e.g.!2
) - If the history command is passed a number as an argument, it outputs that number of previous commands from the history list.
- To execute the most recent command type
!!
and hit Enter. - To execute the most recent iteration of a specific command, type
!command
and hit Enter.
Variables Link to heading
a name (character string), stored temporarily in the system’s memory, to which the user assign a value.
It is nothing more than a reference or pointer to an object (e.g., number, text, filename, or device).
Shell/Local Variables Link to heading
exist only in the current shell. When the user closes a terminal window or shell, all of the local variables are lost.
To set the value of a variable, use the following assignment expression:variable=value
The following example creates a local variable named ‘username’ and assigns it
a value of ‘henry’:
To display the value of the variable, use a dollar sign $
character followed
by the variable name as an argument to the echo
command:
Environment/Global Variables Link to heading
available system-wide and affect the way running processes will behave on a computer (its environment).
- Examples include the
PATH
,HOME
, andHISTSIZE
variables. - The HISTSIZE variable defines how many previous commands to store in the history list.
- The command in the example below teleports you to the home, or user, directory.
- The
env
command outputs a list of the environment variables. - The
export
command is used to turn a shell variable into an environment variable. - The pipe
|
character passes the output of theenv
command to thegrep
command, which searches the output for a pattern and displays the result.
- Exported variables can be removed using the
unset
command.
PATH Variable Link to heading
lists all the directories that the system can look for programs to execute.
The following command displays the path of the current shell, separated by a colon : character.
If the command is not found in any directory listed in the PATH variable, then the shell returns a ‘command not found’ error.
If custom software is installed on the system, it may be necessary to modify the PATH to make it easier to execute these commands. For example, the following will add and verify the /usr/bin/custom directory to the PATH variable:
- When updating the PATH variable, always include the current path, so as not to lose access to commands located in those directories. This can be accomplished by appending $PATH to the value in the assignment expression. Recall that a variable name preceded by a dollar sign represents the value of the variable.
Types of Commands Link to heading
The
type
command can be used to determine information about command type.
Interal Commands Link to heading
(or built-in commands) are built into the shell itself.
External Commands Link to heading
are stored in files that are searched by the shell.
For external commands, the type
command displays the location of the command
by searching the PATH variable (equivalent to the which
command):
External commands can also be executed by typing the complete path to the command.
To display all locations that contain the command name, use the -a
option to
the type
command.
Aliases Link to heading
often used to map longer commands to shorter key sequences.
The type
command can identify aliases to other commands.
To list what aliases are set on the current shell, simply use the alias
commands.
Functions Link to heading
often used to perform repetitive tasks, create new commands, or override commands built-in to the shell or commands stored in files.
Aliases and functions are normally loaded from the initialisation files when the shell first starts.
Quoting Link to heading
Single Quotes: '’ Link to heading
prevent the shell from interpreting metacharacters, including glob characters (wild cards).
In the example below, the shell does not convert the wild card into filenames that match the pattern (like it normally does):
Double Quotes: "" Link to heading
allow for command substitution, variable substitution, and permit some other shell metacharacters.
The following demonstration shows that the value of the PATH variable is still displayed:
Backslash/Escape Character: \ Link to heading
preserves the literal value of the next character that follows.
For example, if a backslash character \ is placed before $1, in double quotes, the shell will not interpret the dollar sign $ metacharacter.
In the following example, the ls
command is aliased to the exa
command for
colour printing. In order to avoid using the alias, place a backslash character
\
in front of your command.
However, a single quote should not occur between single quotes, even when preceded by a backslash.
Backquotes/Backticks: ` Link to heading
used to specify a command within a command, a process called command substitution.
To execute the date
command so the output of that command is sent to the
echo
command, put the date
command inside of two backquotes:
If you do not want the backquotes to be used to execute a command, place single quotes or backslashes around them. However, double quote characters have no effect on backquote characters. The shell will still use them as command substitution:
Control Statements Link to heading
allow you to use multiple commands at once or run additional commands.
Semicolon: ; Link to heading
used to run multiple commands, one after the other. Each command runs independently and consecutively, regardless of the result of the first command.
Double ampersand: && Link to heading
acts as a logical “and”; if the first command is fails, then the second command (to the right of the &&) will not run.
Single pipe: | Link to heading
takes the standard output (stdout) of a command/program/process and passes it on the standard input (stdin) of another command/program/process for further procesing.
Double pipe: || Link to heading
acts as a logical “or”; depending on the result of the first command, the second command will either run or be skipped.
Greater than: > Link to heading
overwrites an already existing file or a new file is created (if the file name is not in the directory).
Double greater than: » Link to heading
appends an already present file or a new file is created (if the file name is not in the directory).
Some Useful Commands Link to heading
Image Source: GoOribi