Fundamentals 12 min read

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

This guide explains why the command‑line interface is called a Shell, clarifies prompts and carriage returns, and walks through essential Bash features such as echo options, quoting rules, variable export, exec vs source, grouping, parameter expansion, arrays, arithmetic, redirection, conditional statements, loops, and control flow operators.

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

Why is it called a Shell?

The operating system kernel cannot be accessed directly; users interact with the kernel through a "shell" program, which serves as a command‑line interface and command interpreter. The shell translates user commands for the kernel and returns the results.

Translate user commands to the kernel.

Translate kernel results back to the user.

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

Shell Prompt (PS1) and Carriage Return (CR)

After logging into a shell, the prompt (e.g., $ for normal users, # for administrators) appears left of the cursor. The prompt indicates that a command can be entered; the command is executed only after a carriage‑return character is read.

Shell Prompt: Ready for input until CR is received.

Carriage Return: Triggers command execution.

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

Echo and Standard Streams

echo

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

Standard streams: stdin (standard input), stdout (standard output), stderr (standard error).
echo -n   # suppress newline
echo -e   # enable backslash escapes

Single vs Double Quotes

Hard quote '' (single quotes) disables all variable and command substitution.

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

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 visible to child processes.

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

exec vs source

Environment variables flow only from parent to child processes. Running a script creates a subshell; source runs the script in the current shell, while exec replaces the current shell with the script.
# run in a subshell
./1.sh
# run in current shell
source 1.sh
# replace current shell
exec 1.sh

Subshell ( ) vs Grouping { }

( )

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

Parameter Expansion Variants

# Assume a 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)

Mnemonic: # removes from the left, % removes 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/all occurrences
${file/dir/path}   # /path1/dir2/dir3/my.file.txt
${file//dir/path}  # /path1/path2/path3/my.file.txt
# Parameter defaults and tests
${file-my.file.txt}   # use my.file.txt if $file is unset
${file:-my.file.txt}  # use my.file.txt if $file is unset or empty
${file+my.file.txt}   # use my.file.txt if $file is set (even empty)
${file:+my.file.txt}  # use my.file.txt if $file is non‑empty
${file=my.file.txt}   # assign my.file.txt if $file is unset
${file:=my.file.txt}  # assign and use my.file.txt if $file is unset or empty
${file?my.file.txt}   # error with message if $file is unset
${file:?my.file.txt}  # same but also prints message to STDERR

Note the distinction between unset, null, and non‑null states.

String Length

${#var}   # length of variable value

Bash Arrays

A=(a b c d)          # define array
${A[@]} ${A[*]}      # all elements
${A[0]}              # first element
${#A[@]} ${#A[*]}   # array length
A[2]=xyz             # modify element

Arithmetic Expansion

a=5;b=7;c=2;
echo $(( a + b * c ))   # prints 19

$@ vs $*

"$@"

expands to separate quoted arguments (e.g., "p1" "p2 p3" "p4"). "$*" expands to a single string containing all arguments separated by the first character of IFS.

Logical Operators && and ||

Two forms of test exist: test expression and [ expression ]. Bash supports string, integer, and file tests. An expression returns 0 (true) or non‑zero (false). command1 && command2 runs command2 only if command1 succeeds (exit status 0). command1 || command2 runs command2 only if command1 fails (non‑zero exit status).

A=123
[ "$A" -n ] && ([ "$A" -lt 100 ] || echo "too big")
unset A

Input/Output Redirection

File descriptors: 0 = stdin, 1 = stdout, 2 = stderr.

Use < to read from a file (replace stdin) and > to write to a file (replace stdout or stderr). Redirection occurs before the command reads input.

ls my.file no.such.file 1>file.out 2>file.err
2>&1   # merge stderr into stdout
ls my.file no.such.file > /dev/null 2>&1
cat < file > file

If vs case

# if example
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
read YN
case "$YN" in
  [Yy]|[Yy][Ee][Ss]) echo "continue" ;;
  *) exit 0 ;;
esac

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
break

exits a loop. return exits a function. exit terminates the script or shell.

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.

Linuxcommand-lineBashVariablesShell scriptingControl structuresRedirection
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.