Module 05 Command Line Skills
Exam Objective 2.1 Command Line Basics
Objective Description
Basics of Using the Linux Command Line
Introduction
Introduction
¡ñ This module will cover the basics of the command line such as:
¡ð The command line interface
¡ð The shell
¡ð Commands
¡ð Variables
¡ð Command Types
¡ð Quoting
¡ð Control Statements
Command Line Interface
Command Line Interface
¡ñ 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 flavor or distribution of Linux
The Shell
The Shell
¡ñ Once a user has entered a command the terminal then accepts what the user has typed and passes to a shell.
¡ñ The CLI provides more precise control, greater speed and the ability to automate tasks more easily through scripting.
¡ñ 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 Shell
¡ñ The Bash shell also has many popular features, a few of which are listed below:
¡ð 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 short nicknames for longer commands.
¡ð Variables
¡ö Used to store information for the Bash shell and for the user.
The Shell
¡ñ When a terminal application is run, and a shell appears, displaying an important part of the interface ¡ª the prompt.
¡ñ Typically the prompt contains information about the user and the system. Below is a common prompt structure:
sysadmin@localhost:~$
¡ñ 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
Commands
¡ñ A command is 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:
sysadmin@localhost:~$
ls
Desktop Documents Downloads Music Pictures Public Templates Videos
Commands
¡ñ 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 behavior of a command.
¡ð Arguments are used to provide additional information (such as a filename or a username).
¡ñ The typical format for a command is as follows:
command [options] [arguments]
Arguments
Commands
¡ñ ¡ñ
command [options] [arguments]
An argument 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:
sysadmin@localhost:~$ ls /etc/ppp ip-down.d ip-up.d
¡ñ
Some commands (such as ls) accept multiple arguments: sysadmin@localhost:~$ ls /etc/ppp /etc/ssh
Options
Options
command [options] [arguments]
¡ñ Options can be used with commands to expand or modify the way a command behaves.
¡ñ For example, using the -l option of the ls command results in a long listing, providing additional information about the files that are listed.
sysadmin@localhost:~$ ls -l
total 0
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Desktop drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Documents Output Omitted…
¡ñ Often the character is chosen to be mnemonic for its purpose, like choosing the letter l for long or r for reverse.
Options
¡ñ Options can be used in conjunction with other options:
sysadmin@localhost:~$ ls -lr
¡ñ 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, like the -h option.
¡ð Full-word options are preceded by two dash — characters like the full-word form of the -h option, the — human-readable option
Command History
¡ñ When a command is executed in the terminal, it is stored in a history list.
¡ñ This makes it easy to execute the same command later 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:
sysadmin@localhost:~$ history 1 date
2 ls
3 cal 5 2030
4 history
Command History
¡ñ 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 (i.e., !3)
¡ñ If the history command is passed a number as an argument, it outputs that number of previous commands from the history list.
sysadmin@localhost:~$ history 3 6 date
7 ls /home
8 history 3
¡ñ 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
Variables
¡ñ A variable is a feature that allows the user or the shell to store data.
¡ñ Variables are given names and stored temporarily in memory.
¡ñ There are two types of variables used in the Bash shell, local and environment.
Local Variables
¡ñ Local or shell, variables exist only in the current shell. When the user closes a terminal window or shell, all of the 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 variable1 and assigns it a value of
Something:
sysadmin@localhost:~$ variable1=’Something’
¡ñ To display the value of the variable, use a dollar sign $ character followed by the variable name as an argument to the echo command:
sysadmin@localhost:~$ echo $variable1 Something
Environment Variables
¡ñ Environment variables, also called global variables, are available system-wide.
¡ñ Examples include the PATH, HOME, and HISTSIZE variables.
¡ñ The command in the example below displays the value of the HISTSIZE variable:
sysadmin@localhost:~$ echo $HISTSIZE 1000
¡ñ The env command outputs a list of the environment variables.
¡ñ The export command is used to turn a local variable into an environment variable.
sysadmin@localhost:~$ export variable1 sysadmin@localhost:~$ env | grep variable1 variable1=Something
¡ñ Exported variables can be removed using the command:
unset
Path Variable
¡ñ One of the most important Bash shell variables to understand is the PATH variable.
¡ñ The PATH variable lists all the places that the system can look for programs to execute.
¡ñ The following command displays the path of the current shell:
sysadmin@localhost:~$ echo $PATH
/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/ games
¡ñ If the command is not found in any directory listed in the PATH variable, then the shell returns a command not found error.
Command Types
Command Types
¡ñ The type command can be used to determine information about command type.
¡ñ There are several different sources of commands within the shell of your CLI:
¡ð Internal commands
¡ð External commands ¡ð Aliases
¡ð Functions
type command
Internal Commands
¡ñ Also called built-in commands, these commands are built into the shell itself.
¡ñ A good example is the cd (change directory) command as it is part of the Bash shell.
¡ñ The type command identifies the cd command as an internal command:
sysadmin@localhost:~$ type cd cd is a shell builtin
External Commands
¡ñ External commands are stored in files that are searched by the shell.
¡ñ It can be beneficial to know where the shell is finding the command or which version
it is using.
¡ñ The which command searches for the location of a command by searching the PATH variable.
sysadmin@localhost:~$ which ls /bin/ls
sysadmin@localhost:~$ which cal /usr/bin/cal
External Commands
¡ñ External commands can be executed by typing the complete path to the command.
sysadmin@localhost:~$ /bin/ls
Desktop Documents Downloads Music Pictures Public Templates Videos
¡ñ For external commands, the type command displays the location of the command:
sysadmin@localhost:~$ type cal cal is /usr/bin/cal
¡ñ To display all locations that contain the command name, use the -a option to the type command:
sysadmin@localhost:~$ type -a echo
Aliases
¡ñ An alias can be used to map longer commands to shorter key sequences.
¡ñ For example, the command ls -l is commonly aliased to l or ll.
¡ñ To determine what aliases are set on the current shell use the alias command:
sysadmin@localhost:~$ alias alias egrep=’egrep –color=auto’ alias fgrep=’fgrep –color=auto’ Output Omitted…
¡ñ The type command can identify aliases to other commands:
sysadmin@localhost:~$ type ll ll is aliased to `ls -alF’
Functions
¡ñ Functions can also be built using existing commands to:
¡ð Create new commands
¡ð Override commands built-in to the shell or commands stored in files
¡ñ Aliases and functions are normally loaded from the initialization files when the shell first starts.
Quoting
Double Quotes
¡ñ Double quotes stop the shell from interpreting some metacharacters, including glob characters.
Glob characters, also called wild cards, are symbols that have special meaning to the shell (i.e, *, ?).
¡ñ This is useful when you want to display something on the screen that is normally a
special character to the shell.
¡ñ In the example below, the Bash shell doesn’t convert the glob pattern into filenames that match the pattern (like it normally does):
sysadmin@localhost:~$ echo “The glob characters are *, ? and [ ]” The glob characters are *, ? and [ ]
¡ñ Double quotes still allow for command substitution, variable substitution, and permit some other shell metacharacters (i.e., the PATH variable)
Single Quotes
¡ñ Single quotes prevent the shell from doing any interpreting of special characters, including globs, variables, command substitution and other metacharacters.
sysadmin@localhost:~$ echo The car costs $100 The car costs 00
sysadmin@localhost:~$ echo ‘The car costs $100’ The car costs $100
Backslash Character
¡ñ A technique to essentially single quote a single character is to use the backslash character \.
¡ñ If the phrase below is placed in single quotes, $1and $PATH are not variables:
sysadmin@localhost:~$ echo “The service costs $1 and the path is $PATH”
The service costs and the path is /usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin :/usr/games
¡ñ What if you want to have $PATH treated as a variable and $1 not?
¡ñ In this case, use a backslash \ character in front of the dollar sign $ character to prevent the shell from interpreting it:
sysadmin@localhost:~$ echo The service costs \$1 and the path is $PATH
The service costs $1 and the path is /usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bi n:/usr/games
Backquotes
¡ñ Backquotes, or backticks, are used to specify a command within a command, a process called command substitution.
¡ñ Note the output of the echo Today is date command line:
sysadmin@localhost:~$ echo Today is date Today is date
¡ñ 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:
sysadmin@localhost:~$ echo Today is `date` Today is Mon Nov 4 03:40:04 UTC 2030
Control Statements
Control Statements
¡ñ Control statements allow you to use multiple commands at once or run additional commands.
¡ñ Control statements include:
¡ð Semicolon (;)
¡ð Double ampersand (&&)
¡ð Double pipe (||)
Control Statements
¡ñ The semicolon can be used to run multiple commands, one after the other:
sysadmin@localhost:~$ cal 1 2015; cal 2 2015; cal 3 2015
¡ñ The double ampersand && acts as a logical “and” if the first command is successful, then the
second command (to the right of the &&) will also run:
sysadmin@localhost:~$ ls /etc/xml && echo success catalog catalog.old xml-core.xml xml-core.xml.old success
¡ñ The double pipe || is a logical “or”. It works similarly to &&; depending on the result of the first command, the second command will either run or be skipped:
sysadmin@localhost:~$ ls /etc/junk || echo failed
ls: cannot access /etc/junk: No such file or directory failed