Mastering Linux Shell: Key Concepts, Commands, and Scripting Tricks
This article explains the fundamentals of Linux shells, covering why they’re called shells, prompt behavior, echo usage, quoting differences, variable handling, export, command grouping, arithmetic expansion, special parameters, redirection, conditional statements, loops, and provides practical Bash code examples for each concept.
1. Why is it called a Shell?
Users cannot interact directly with the kernel; they use a "shell" as an interface that translates commands into kernel actions and returns results. The shell is essentially a command interpreter.
Translates user commands for the kernel.
Translates kernel results back to the user.
Different operating systems have different kernels, and multiple shells (sh, bash, csh, ksh, etc.) can run on the same kernel.
2. Shell Prompt (PS1) and Carriage Return (CR)
After logging into a shell, the prompt appears left of the cursor. Ordinary users see $, while administrators see #.
Shell Prompt: input is accepted until a CR (Carriage Return) character is read.
Carriage Return: triggers execution of the entered command.
Technically, the shell splits the input line using IFS, processes meta‑characters, and then reassembles the command.
3. The echo Command
echosends its arguments to standard output (stdout), typically the screen.
stdin – standard input stdout – standard output stderr – standard error
echo -n # suppress trailing newline
echo -e # enable backslash escapes4. Double Quotes vs. Single Quotes
''(single quotes) – hard quoting; disables all variable and command substitution. "" (double quotes) – soft quoting; allows $ variable expansion.
5. var=value vs. export var=value
Variable definition: name=value (no spaces around =).
Variable reference: echo ${name}. export name=value makes the variable an environment variable, inheritable by child processes.
# Local variable
A=B
# Unset variable
unset A
# Export as environment variable
export A=B6. exec vs. source
Environment variables flow only from parent to child processes. Executing a script creates a subshell; source runs the script in the current shell.
# Run script in a new subshell
./1.sh
# Run script in the current shell
source 1.sh
# Replace current shell with script
exec 1.sh7. ( ) vs. { }
( )groups commands in a subshell; { } groups commands in the current shell.
8. $(()) , $() , and ${} Differences
# Assume variable
file=/dir1/dir2/dir3/my.file.txt
# Non‑greedy left delete
${file#*/} # dir1/dir2/dir3/my.file.txt
# Greedy left delete
${file##*/} # my.file.txt
${file##*.} # txt
# Non‑greedy right delete
${file%/*} # /dir1/dir2/dir3
${file%.*} # /dir1/dir2/dir3/my.file
# Greedy right delete
${file%%/*} # (empty)
${file%%.*} # /dir1/dir2/dir3/my
# Substring extraction
${file:0:5} # /dir1
${file:5:5} # /dir2
# Replace first occurrence
${file/dir/path} # /path1/dir2/dir3/my.file.txt
# Replace all occurrences
${file//dir/path} # /path1/path2/path3/my.file.txt
# Parameter defaults
${file-my.file.txt} # if $file unset, use my.file.txt
${file:-my.file.txt} # if $file unset or empty, use my.file.txt
${file+my.file.txt} # if $file set (even empty), use my.file.txt
${file:+my.file.txt} # if $file non‑empty, use my.file.txt
${file=my.file.txt} # assign default if unset
${file:=my.file.txt} # assign default if unset or empty
${file?my.file.txt} # error if unset
${file:?my.file.txt} # error if unset or empty9. $@ vs. $*
"$@"expands to separate quoted arguments (e.g., "p1" "p2 p3" "p4"). "$*" expands to a single string with all arguments concatenated (e.g., "p1 p2 p3 p4").
10. && vs. ||
The test command can be written as test expression or [ expression ]. Bash supports testing strings, integers, and files. It returns 0 for true, non‑zero for false. command1 && command2 runs command2 only if command1 succeeds (return 0). command1 || command2 runs command2 only if command1 fails (non‑zero).
A=123
[ -n "$A" ] && ([ "$A" -lt 100 ] || echo "too big")
unset A11. Redirection: > vs. <
0: STDIN 1: STDOUT 2: STDERR
<redirects input from a file; > redirects output to a file (stdout or stderr).
ls my.file no.such.file 1>file.out 2>file.err
# Merge stderr into stdout
ls my.file no.such.file 1>file.out 2>&1
# Discard output
ls my.file no.such.file > /dev/null 2>&1
# Example of order: > file clears file before < file reads, causing empty input.
cat < file > file12. if vs. case
# if example
echo -n "Do you want to continue?(Yes/No):"
read YN
if [ "$YN" = Y -o "$YN" = y -o "$YN" = Yes -o "$YN" = yes -o "$YN" = YES ]; then
echo "continue"
else
exit 0
fi
# case example
echo -n "Do you want to continue?(Yes/No):"
read YN
case "$YN" in
[Yy]|[Yy][Ee][Ss]) echo "continue";;
*) exit 0;;
esac13. Loops: for , while , until
# for loop
for ((i=1;i<=10;i++))
do
echo "num is $i"
done
# while loop
num=1
while [ "$num" -le 10 ]; do
echo "num is $num"
num=$((num + 1))
done
# until loop
num=1
until [ "$num" -gt 10 ]; do
echo "num is $num"
num=$((num + 1))
done breakexits a loop. return exits a function. exit terminates the script or shell.
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.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.
