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' commandEdit .bash_history manually:
nano ~/.bash_historyInvoke 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/binImportant Linux Bash Commands and Tools
| Command | Purpose & Example |
|---|---|
bash | Start a bash shell: bash |
echo | Print to screen: echo "Hello $USER" |
env | Show current environment: env |
export | Export variable: export VAR=value |
pwd | Print current directory: pwd |
set | Set or show shell variables: `set |
unset | Remove variable: unset VAR |
type | Show how command is interpreted: type ls |
which | Show full path of command: which bash |
man | Manual pages: man ls, man bash |
uname | Info about system: uname -a |
history | List shell history: history |
.bash_history | File storing history: cat ~/.bash_history |
Quoting in Bash (VERY Important)
| Type | Description | Example |
|---|---|---|
Double " | Expands variables | echo "User: $USER" |
Single ' | Literal value, no expansion | echo 'User: $USER' |
Backticks ` or $(...) | Command substitution | echo $(date) |
Give this command a shot:
export MYNAME="Linux Admin" && echo "Welcome $MYNAME" && date >> ~/session.log && type echoWhat 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 alexInside 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
