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:
pwdprints 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:
typeunderstands shell builtins such ascdwhichshows where a normal executable command was found
Why both matter
typeunderstands shell builtins and aliaseswhichlooks for executable files inPATH
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:
$HOMEpoints to the home directory$PATHis 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
- Run:
type cd - Notice that
cdis a shell builtin. - Then run:
which ls - Notice that
lsis found as a normal executable.
2) Create and export a variable
- Run:
MYVAR=hello - Check it:
echo "$MYVAR" - Export it:
export MYVAR - Confirm it appears in:
env | grep MYVAR
Cheat Sheet
pwd= print current directoryls -la= list files with detailstype= show how the shell understands a commandwhich= find command inPATHenv= show environmentexport= pass a variable to child processesunset= remove a variablehistory= show previous commandsman= 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.
