Fundamentals 11 min read

Master Linux Shell: Core Concepts, Commands, and Scripting Tricks

This guide explains what a Linux shell is, how prompts and carriage returns work, the usage of echo, quoting rules, variable assignment and export, differences between exec and source, command grouping, parameter expansion, arrays, test operators, I/O redirection, and control‑flow structures, providing concise examples for each.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Shell: Core Concepts, Commands, and Scripting Tricks

1. Why is it called a Shell?

Users cannot interact directly with the kernel; they use a "shell"—a command‑line interface that translates user commands for the kernel and returns the results.

Translate user commands for the kernel.

Translate kernel results back to the user.

Different operating systems have different kernels, and the same kernel can be used with various shells such as sh, bash, csh, ksh, etc.

2. Relationship between 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: you can type a command; the command is read until a CR character is received.

Carriage Return: the command is executed.

Technically, the shell splits the input line into fields using IFS, processes meta‑characters, and then reassembles the command.

3. Echo basics

echo

sends its arguments to standard output (stdout), usually the screen.

stdin – standard input stdout – standard output stderr – standard error output
echo -n   # suppress newline
echo -e   # enable backslash escapes

4. Difference between double quotes "" and single quotes ''

Hard quote '' (single quotes) disables all variable expansion.

Soft quote "" (double quotes) preserves $ expansion.

5. Variable assignment and export

Define a variable: name=value (no spaces around =).

Reference a variable: echo ${name}.

Export a variable: export name=value makes it an environment variable.

# local variable
A=B
# delete variable
unset A
# export as environment variable
export A=B

6. exec vs source

Environment variables are passed only from parent to child processes. Executing a script creates a subshell; source runs the script in the current shell.
# run in a new subshell
./1.sh
# run in current shell
source 1.sh
# replace current shell with script
exec 1.sh

7. ( ) vs { }

( )

runs the command group in a subshell; { } runs it in the current shell.

8. Parameter expansion forms

# Example 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
# non‑greedy right delete
${file%/*}   # /dir1/dir2/dir3
# greedy right delete
${file%%/*}  # (empty)

Use # to remove from the left, % to remove from the right; a single symbol means minimal match, double means maximal match.

# 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

9. $@ vs $*

"$@"

expands to separate words: "p1" "p2 p3" "p4". "$*" expands to a single word: "p1 p2 p3 p4".

10. && vs ||

Both are logical operators used with the test command. command1 && command2 runs command2 only if command1 succeeds (return 0). command1 || command2 runs command2 only if command1 fails (non‑zero).

11. > vs < redirection

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
# combine stderr into stdout
1>&2
# discard output
ls my.file no.such.file >/dev/null 2>&1

12. if vs case

# if example
read YN
if [ "$YN" = Y -o "$YN" = y -o "$YN" = "Yes" ]; then
  echo "continue"
else
  exit 0
fi
# case example
read YN
case "$YN" in
  [Yy]|[Yy][Ee][Ss]) echo "continue" ;;
  *) exit 0 ;;
esac

13. for, while, until loops

# 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
break

exits a loop. return exits a function. exit ends the script.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxScripting
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

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.