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 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.
  • 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

  • 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:

Arguments

Commands

  • 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 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.
  • 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: 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:
  • 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.
  • 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:

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:
  • The env command outputs a list of the environment variables.
  • The export command is used to turn a local variable into an environment variable.
  • Exported variables can be removed using the unset command:

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:
  • 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 : type command
  • There are several different sources of commands within the shell of your CLI:
    • Internal commands
    • External commands
    • Aliases
    • Functions

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:

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.
  • External commands can be executed by typing the complete path to the command.
  • For external commands, the type command displays the location of the command:
  • To display all locations that contain the command name, use the -a option to the type command:

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:
  • The type command can identify aliases to other commands:

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):
  • Double quotes still allow for command substitutionvariable 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.

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:
  • 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:

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:
  • 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:

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 (||)
  • The semicolon can be used to run multiple commands, one after the other:
  • 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:
  • 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:
Video 5.1 How to use the Command Line

Berikut file materi yang bisa di download :