103.1 Work on the Command Line

Mastering Command Line Basics: LPIC-1 Exam Objective 103.1 #

As you prepare for the LPIC-1 certification, mastering the command line is essential. This tutorial covers key aspects of interacting with shells and commands using the command line, focusing on the Bash shell. We’ll delve into single shell commands, shell environment modification, command history, and invoking commands inside and outside the defined path.

Single Shell Commands and One-Line Command Sequences #

Basic Shell Commands #

  1. echo: Print text to the terminal.

    echo "Hello, World!"
    

    Output:

    Hello, World!
    
  2. pwd: Print the current working directory.

    pwd
    

    Output:

    /home/user
    
  3. uname: Display system information.

    uname -a
    

    Output:

    Linux hostname 5.4.0-74-generic #83-Ubuntu SMP Mon May 10 19:18:00 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
    

One-Line Command Sequences #

Combine multiple commands using the ; separator.

mkdir test_dir; cd test_dir; touch file1 file2; ls

This sequence creates a directory, navigates into it, creates two files, and lists them.

Shell Environment #

Defining, Referencing, and Exporting Environment Variables #

  1. Defining a variable:

    MY_VAR="Hello"
    
  2. Referencing a variable:

    echo $MY_VAR
    

    Output:

    Hello
    
  3. Exporting a variable:

    export MY_VAR
    
  4. Using env to view environment variables:

    env | grep MY_VAR
    

    Output:

    MY_VAR=Hello
    
  5. Unsetting a variable:

    unset MY_VAR
    

Command History #

  1. Viewing command history:

    history
    
  2. Re-running a command from history:

    !42
    

    This runs the command numbered 42 in the history.

  3. Editing history with history -c:

    history -c
    

    This clears the command history.

Invoking Commands #

Inside the Defined Path #

Commands within the system’s PATH can be executed directly.

  1. Using which to locate executables:

    which ls
    

    Output:

    /bin/ls
    
  2. Using type to display command type:

    type ls
    

    Output:

    ls is aliased to `ls --color=auto`
    

Outside the Defined Path #

To run a script not in the PATH, specify its path.

  1. Running a script from the current directory:
    ./script.sh
    

Quoting in Shell #

Quoting controls the interpretation of special characters.

  1. Double quotes ("):

    echo "The value of HOME is $HOME"
    

    Output:

    The value of HOME is /home/user
    
  2. Single quotes ('):

    echo 'The value of HOME is $HOME'
    

    Output:

    The value of HOME is $HOME
    
  3. Backslash (\) for escaping:

    echo "A quote: \""
    

    Output:

    A quote: "
    

Summary #

This tutorial covered the essentials of working on the command line with Bash, including single shell commands, modifying the shell environment, using command history, and invoking commands inside and outside the defined path. Mastering these basics is crucial for success in the LPIC-1 exam and for efficient daily use of the Linux command line. Happy learning!