Linucate
~ Linucate_

103.1 Work on the command line

All Levels

Introduction

This lesson is about feeling comfortable in the shell.

The main question is:

How do you move around, run commands, and control your shell environment without getting lost?

LPIC-1 assumes Bash.

What you should be able to do after this lesson:

  • Run commands inside and outside PATH.
  • Use environment variables.
  • Read command history.
  • Understand simple quoting.
  • Use help tools such as man.

Big Idea: The Shell Is a Command Workspace

When you type a command, the shell decides:

  • what command you meant
  • where to find it
  • how to expand variables
  • how to treat quotes

That is why tiny details like $PATH and quoting matter.

Where Am I?

Print current directory

pwd

Example output:

/home/<user>/projects/lpic

What this shows:

  • pwd prints the full path of the current working directory
  • the exact path depends on the system and the current location

List files

ls
ls -la

Commands Inside and Outside PATH

Check what a command is

type cd
which ls

Example output:

cd is a shell builtin
/usr/bin/ls

What this shows:

  • type understands shell builtins such as cd
  • which shows where a normal executable command was found

Why both matter

  • type understands shell builtins and aliases
  • which looks for executable files in PATH

Run a command outside PATH

./script.sh
/usr/local/bin/mytool

One-Line Command Sequences

LPIC also expects comfort with simple one-line command sequences.

Examples:

pwd; ls -la
cd /tmp && ls

Simple idea:

  • ; runs the next command after the previous one finishes
  • && runs the next command only if the previous one succeeded

Environment Variables

Variables hold values the shell and programs can use.

Show environment

env | head
set | head

Show one variable

echo "$HOME"
echo "$PATH"

Sanitized example output:

/home/<user>
/home/<user>/.local/bin:/usr/local/bin:/usr/bin

What this shows:

  • $HOME points to the home directory
  • $PATH is the list of directories searched for commands

Create and export

EDITOR=nano
export EDITOR

Remove a variable

unset EDITOR

Simple rule:

  • shell variable = local to the shell unless exported
  • exported variable = also passed to child processes

Command History

The shell remembers commands.

Show history

history | tail

Rerun from history

  • !! = previous command
  • !123 = command number 123

History file

Common Bash history file:

~/.bash_history

Quoting: Very Important for Beginners

No quotes

The shell expands spaces and variables normally.

Single quotes

echo '$HOME'

This prints $HOME literally.

Double quotes

echo "$HOME"

This expands the variable but protects spaces better than no quotes.

Simple memory trick:

  • single quotes = treat text more literally
  • double quotes = still allow variable expansion

Help and Information

Manual pages

man ls
man bash

System information

uname -a

Practice Step by Step

1) Check how the shell sees a command
  1. Run: type cd
  2. Notice that cd is a shell builtin.
  3. Then run: which ls
  4. Notice that ls is found as a normal executable.
2) Create and export a variable
  1. Run: MYVAR=hello
  2. Check it: echo "$MYVAR"
  3. Export it: export MYVAR
  4. Confirm it appears in: env | grep MYVAR

Cheat Sheet

  • pwd = print current directory
  • ls -la = list files with details
  • type = show how the shell understands a command
  • which = find command in PATH
  • env = show environment
  • export = pass a variable to child processes
  • unset = remove a variable
  • history = show previous commands
  • man = open manual pages
  • single quotes = more literal text
  • double quotes = allow variable expansion
🎯

Test Your Knowledge

Complete the quiz to assess your understanding of this course's concepts.