Fundamentals 13 min read

Master Linux Shell: Essential Commands, Prompt Customization, and Session Management

This guide explains what a Linux shell is, how to view and set the current shell and hostname, customize the command prompt, differentiate internal and external commands, manage aliases, use common system commands, handle sessions with screen and tmux, employ echo options and ANSI codes, leverage bash shortcuts, apply file globbing patterns, and securely delete files with shred.

Raymond Ops
Raymond Ops
Raymond Ops
Master Linux Shell: Essential Commands, Prompt Customization, and Session Management

1 What is a shell

Shell is the user interface of Linux, a command interpreter that receives user commands and passes them to the kernel for execution.

<code>echo ${SHELL}
# /bin/bash</code>

To view all shells installed on the system:

<code>cat /etc/shells
# /bin/sh
# /bin/bash
# /usr/bin/sh
# /usr/bin/bash
# /bin/tcsh
# /bin/csh</code>

2 Setting the hostname

Temporary change:

<code>hostname localhost</code>

Permanent change (CentOS 7 / Ubuntu 18.04+):

<code>hostnamectl set-hostname localhost</code>

3 Command prompt

Prompt format placeholders:

\e – escape 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

Example to view current PS1:

<code>cat $PS1</code>

Persist prompt on CentOS:

<code>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.sh</code>

Persist prompt on Ubuntu:

<code>echo "PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\]'" >> .bashrc</code>

4 Internal vs external commands

Internal commands are built into the shell and reside in memory; external commands are executable files on disk loaded only when invoked.

To list internal commands:

<code>help</code>

Enable or disable an internal command:

<code>enable command      # enable
enable -n command   # disable
enable -n           # list disabled internal commands</code>

To locate an external command:

<code>which -a --skip-alias ${command}</code>

5 Command aliases

Define short aliases for frequently used commands:

<code>alias                # list all aliases
alias name='command' # create alias
unalias name         # remove alias
unalias -a           # remove all aliases</code>

Aliases defined in the current shell are temporary; to make them permanent, add them to

~/.bashrc

(user) or

/etc/bashrc

(system) and reload with:

<code>source ~/.bashrc
source /etc/bashrc</code>

6 Common commands

Examples of frequently used system queries:

<code>lscpu
cat /proc/cpuinfo
free
cat /proc/meminfo
lsblk
cat /proc/partitions
arch
uname -r
cat /etc/os-release
lsb_release -a
halt
poweroff
reboot
shutdown -h now
whoami
who
w</code>

7 Session management

Screen:

<code>screen -ls               # list sessions
screen -S name           # create session
Ctrl+a d                 # detach
screen -x name          # attach
exit                     # close session
screen -r name          # resume session</code>

Tmux (more powerful terminal multiplexer):

<code>tmux new -s session_name
tmux detach
Ctrl+b d
tmux attach -t <session-name>
tmux kill-session -t <session-name>
tmux switch -t <session-name>
tmux split-window
tmux split-window -h
tmux list-keys
tmux list-commands</code>

Key bindings for pane management (e.g., Ctrl+b % for vertical split, Ctrl+b " for horizontal split, Ctrl+b arrow keys to move between panes, Ctrl+b x to close a pane, etc.) are also listed.

8 Echo output

Syntax:

<code>echo [-neE] [string]</code>

Options:

-E (default) – disable backslash interpretation

-n – omit trailing newline

-e – enable backslash interpretation

Common escape sequences when -e is used include \a (bell), \b (backspace), \c (suppress newline), \e (escape), \n (newline), \r (carriage return), \t (tab), \\ (backslash), \0nnn (octal), \xHH (hex).

ANSI color codes example:

<code>"\033[background;colormString\033[0m"</code>

Foreground colors 30‑37, background colors 40‑47, with typical mappings (30/40 black, 31/41 red, 32/42 green, 33/43 yellow, 34/44 blue, 35/45 magenta, 36/46 cyan, 37/47 white). Additional control codes such as \033[0m (reset), \033[1m (bold), \033[4m (underline), cursor movement, clear screen, hide/show cursor, etc., are also described.

9 Bash shortcuts

Common key combinations for editing and navigating the command line are listed, including Ctrl+L (clear screen), Ctrl+C (abort), Ctrl+U (delete to start), Alt+f (forward word), Alt+b (backward word), Ctrl+R (reverse search), and many others.

10 File globbing

Pattern examples:

<code>*      # matches any number of characters except leading .
?      # matches any single character
~      # home directory
[0-9]  # digit range
[!a-z] # characters not in range</code>

POSIX character classes such as [:digit:], [:lower:], [:upper:], [:alpha:], [:alnum:], [:blank:], [:space:], [:punct:], [:print:], [:cntrl:], [:graph:], [:xdigit:] are also shown.

11 Secure file deletion

Use

shred

instead of

rm

for secure deletion. Common options:

-z – add a final overwrite with zeros

-v – verbose output

-u – truncate and delete after overwriting

-n N – overwrite N times (default 3)

LinuxShellCommand-lineSysadminbashTerminal
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.