The work on the Linux command line topic is fundamental to your day-to-day work as a Linux administrator. Below is a breakdown of each area and command, with simple explanations and practical command-line examples you can try. But, first I need to brew coffee at home – Very important!

Use single shell commands and one-line command sequences

You can chain commands together using:

  • ; to run commands sequentially
  • && to run next command only if the previous one succeeds
  • || to run next command only if the previous one fails

Give it a shot, open your Terminal and run the below Linux commands examples:

echo "Hello" ; echo "World"
mkdir test && cd test
ping -c 1 8.8.8.8 || echo "Ping failed"

Use and modify the shell environment Define, reference, and export environment variables

NAME="Linux"
echo $NAME           # Referencing variable
export CITY="NYC"    # Makes it available to sub-processes
env | grep CITY      # See exported variable
unset NAME           # Removes the variable

Use and edit command history

history              # List previous commands
!42                  # Run command number 42
!!                   # Repeat last command
!ls                  # Run last 'ls' command

Edit .bash_history manually:

nano ~/.bash_history

Invoke commands inside and outside the path

echo $PATH                     # Shows where shell looks for executables
ls /usr/bin                   # Inside PATH
./my_script.sh                # Outside PATH, run with relative path

To add your own folder to PATH:

export PATH=$PATH:$HOME/bin

Important Linux Bash Commands and Tools

CommandPurpose & Example
bashStart a bash shell: bash
echoPrint to screen: echo "Hello $USER"
envShow current environment: env
exportExport variable: export VAR=value
pwdPrint current directory: pwd
setSet or show shell variables: `set
unsetRemove variable: unset VAR
typeShow how command is interpreted: type ls
whichShow full path of command: which bash
manManual pages: man ls, man bash
unameInfo about system: uname -a
historyList shell history: history
.bash_historyFile storing history: cat ~/.bash_history

Quoting in Bash (VERY Important)

TypeDescriptionExample
Double "Expands variablesecho "User: $USER"
Single 'Literal value, no expansionecho 'User: $USER'
Backticks ` or $(...)Command substitutionecho $(date)

Give this command a shot:

export MYNAME="Linux Admin" && echo "Welcome $MYNAME" && date >> ~/session.log && type echo

What do you mean double ” expands variables?

When we say double quotes " " expand variables, we mean that the shell will interpret variables or commands inside the quotes, instead of treating them as plain text.

Brew a Caturra coffee variety and run this example next:

USER="alex"
echo "Hello $USER"
Output will be:
Hello alex

Inside double quotes "Hello $USER", the $USER is expanded to its value: alex.

But with single quotes ' ', nothing is expanded:

USER="alex"
echo 'Hello $USER'

Output will be:

Hello $USER

Leave a comment

Your email address will not be published. Required fields are marked *