Master Linux Shell: Essential Commands, Prompt Customization, and Session Management
This guide introduces the Linux shell, explains how to view and set the current shell and hostname, customize the command prompt, differentiate internal and external commands, manage aliases, use common utilities, handle sessions with screen and tmux, and employ advanced echo formatting and safety deletion techniques.
1 What is a shell
Shell is the user interface of Linux, providing an interface for users to interact with the kernel. It receives commands and passes them to the kernel for execution, thus also called the Linux command interpreter.
Display the current shell in use
echo ${SHELL}
# /bin/bashView all shells available on the system
cat /etc/shells
# /bin/sh
# /bin/bash
# /usr/bin/sh
# /usr/bin/bash
# /bin/tcsh
# /bin/csh2 Set hostname
# Temporary effect
hostname localhost
# Persistent effect (CentOS7 and Ubuntu 18.04+)
hostnamectl set-hostname localhost3 Command prompt
Prompt format symbols:
\e control character \033
\u current user
\h short hostname
\H full hostname
\w current working directory
\W basename of current directory
\t 24‑hour time
\T 12‑hour time
! command history number
... (boot‑time command history number)
View current PS1
cat $PS1Persist prompt on CentOS
echo 'PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"' > /etc/profile.d/env.shPersist prompt on Ubuntu
echo "PS1='\[\e[1;35m\][\u@\h \W]\\$'\[\e[0m\]'" >> .bashrc4 Internal vs external commands
Internal commands are built into the shell and reside in memory after login.
External commands are executable files on the filesystem; they are loaded from disk only when invoked.
Check command type
type commandInternal command management
help # list all internal commands
enable command # enable an internal command
enable -n command # disable an internal command
enable -n # list disabled internal commandsExternal command management
# Show the full path of an external command
which -a --skip-alias ${command}Hash table caching
The system starts with an empty hash table. When an external command is executed, its path is stored in the hash table, speeding up subsequent calls.
hash # display hash cache
hash -l # list hash entries
hash -p path name # assign an alias to a full path
hash -t name # print the path of name in cache
hash -d name # delete cache entry
hash -r # clear the entire hash table
5 Command aliases
Define short aliases for frequently used long commands.
alias # list all aliases
alias name='command' # create alias
unalias name # remove specific alias
unalias -a # remove all aliasesAliases defined in the current shell are temporary; to make them permanent, add them to configuration files such as ~/.bashrc (user‑only) or /etc/bashrc (all users). After editing, reload with:
source ~/.bashrc
source /etc/bashrc6 Common commands
lscpu
cat /proc/cpuinfo
free
cat /proc/meminfo
lsblk
cat /proc/partitions
arch
uname -r
cat /etc/os-release
cat /etc/redhat-release
lsb_release -a
halt
poweroff
reboot
shutdown -h now
whoami
who
w7 Session management
Screen commands
screen -ls # list sessions
screen -S name # create session
Ctrl+a d # detach session
screen -x name # attach to session
screen -r name # resume session
exit # close sessionTmux overview
Multiple sessions can be accessed from a single window.
New windows can attach to existing sessions.
Sessions can be shared by multiple users.
Supports vertical and horizontal pane splitting.
# New session
tmux new -s session_name
# Detach
tmux detach
Ctrl+b d
# Attach
tmux attach -t <session-name>
# Kill session
tmux kill-session -t <session-name>
# Switch session
tmux switch -t <session-name>
# Split panes
tmux split-window # vertical
tmux split-window -h # horizontal
# List keys and commands
tmux list-keys
tmux list-commands8 echo output
Syntax: echo [-neE] [string] -E (default) disables backslash interpretation.
-n suppresses the trailing newline.
-e enables backslash escape sequences.
Common escape sequences when -e is used:
\a alert (bell)
\b backspace
\c suppress final newline
\e escape (equivalent to \033)
\n newline
\r carriage return
\t horizontal tab
\\ backslash
\0nnn octal ASCII
\xHH hexadecimal ASCII
ANSI color codes example:
"\033[background;foregroundmString\033[0m"Foreground colors 30‑37, background colors 40‑47 (e.g., 31 = red, 44 = blue).
Other common ANSI control codes include resetting attributes, cursor movement, screen clearing, etc.
9 Bash shortcuts
Ctrl+l clear screen
Ctrl+o execute line and redisplay
Ctrl+s stop output (lock)
Ctrl+q resume output (unlock)
Ctrl+c terminate command
Ctrl+z suspend command
Ctrl+a move cursor to line start
Ctrl+e move cursor to line end
Ctrl+f move cursor forward one character
Ctrl+b move cursor backward one character
Alt+f move forward one word
Alt+b move backward one word
Ctrl+u delete to line start
Ctrl+k delete to line end
Alt+r delete current line
Ctrl+w delete previous word
Alt+d delete next word
Alt+Backspace delete left word
Ctrl+d delete character under cursor
Ctrl+h delete character before cursor
Ctrl+y yank (paste) deleted text
Alt+c capitalize word
Alt+u uppercase word
Alt+l lowercase word
Ctrl+t transpose characters
Alt+t transpose words
Alt+# repeat character # times10 File globbing
* matches zero or more characters (excluding leading .)
? matches any single character
~ home directory
~user specific user's home
. and ~+ current directory
~- previous directory
[0-9] numeric range
[a-z] lowercase letters
[A-Z] uppercase letters
[wang] any of w,a,n,g
[^wang] any character except w,a,n,g
[:digit:] digits, etc.11 Secure file deletion
Use shred for secure deletion.
-z final overwrite with zeros
-v verbose output
-u truncate and delete after overwriting
-n # number of overwrites (default 3)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
