Operations 13 min read

Mastering Linux Shell: 13 Essential Concepts Every Developer Should Know

This article explains the fundamentals of Linux shells, covering why they’re called shells, prompt and carriage return behavior, echo command, quoting rules, variable assignment and export, differences between exec and source, grouping syntax, arithmetic expansion, parameter handling, loops, conditionals, and I/O redirection.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering Linux Shell: 13 Essential Concepts Every Developer Should Know

1. Why is it called Shell?

Computers rely on hardware, but users cannot operate hardware directly; the operating system (OS) controls the hardware. Linux is, strictly speaking, an OS.

Users do not interact with the kernel directly; they use a "shell"—the kernel’s outer program—to communicate. The shell is a user‑system interface that works via the command line.

The simplest definition of a shell is a command interpreter .

Translates user commands for the kernel to process.

Translates kernel results back to the user.

Different OSes use different kernels, and the same kernel can have different shells (e.g., sh, bash, csh, ksh).

2. Shell Prompt (PS1) and Carriage Return (CR)

After logging into a shell, the text left of the cursor is the prompt. Regular users see $, while administrators see #.

Shell Prompt: you can type a command; the command is read until a Carriage Return (CR) character is encountered.

Carriage Return: the command is executed.

Technically, the shell splits the entered command line into fields using IFS (Internal Field Separator), processes special characters, and then reassembles the line.

3. Echo Command

echo

sends its arguments to standard output (stdout), usually displayed on 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) disables all variable expansion. "" (soft quote) preserves $ expansion.

5. var=value vs. export

Variable definition: name=value (no spaces around =).

Variable substitution: echo ${name}.

Exported variable: export name=value makes it an environment variable.

# Local variable
A=B
# Unset variable
unset A
# Exported variable
export A=B

6. Difference Between exec and source

Environment variables are passed only from parent to child processes. When a shell script is executed, a subshell (child process) is created; source runs the script in the current shell.
# Create a subshell to run the script
./1.sh
# Run in current shell
source 1.sh
# Replace current shell with script
exec 1.sh

7. Difference Between ( ) and { }

( )

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

8. Difference Between $(()), $(), and ${}

# 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
${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
${file//dir/path}  # /path1/path2/path3/my.file.txt
# Parameter defaults
${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 if unset or empty
${file?my.file.txt}   # error if $file is unset
${file:?my.file.txt}  # error if $file is unset or empty

9. Difference Between $@ and $*

"$@"

expands to separate quoted arguments (e.g., "p1" "p2 p3" "p4"). "$*" expands to a single string containing all arguments (e.g., "p1 p2 p3 p4").

10. Difference Between && and ||

Both are logical operators for command chaining. command1 && command2 runs command2 only if command1 returns exit status 0 (true). command1 || command2 runs command2 only if command1 returns a non‑zero (false) status.

11. Difference Between > and <

0: STDIN, 1: STDOUT, 2: STDERR
<

redirects input from a file to STDIN; > redirects output (STDOUT or STDERR) to a file.

ls my.file no.such.file 1> file.out 2>file.err
ls my.file no.such.file 1> file.out 2>&1
ls my.file no.such.file >/dev/null 2>&1
cat < file > file   # note: > file truncates before < file reads

12. Choosing if vs. case

# if
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
echo -n "Do you want to continue?(Yes/No):"
read YN
case "$YN" in
  [Yy]|[Yy][Ee][Ss]) echo "continue" ;;
  *) exit 0 ;;
esac

13. Loops: for, while, until

# for
for ((i=1;i<=10;i++))
do
  echo "num is $i"
done

# while
num=1
while [ "$num" -le 10 ]; do
  echo "num is $num"
  num=$(($num + 1))
done

# until
num=1
until [ "$num" -gt 10 ]; do
  echo "num is $num"
  num=$(($num + 1))
done

Additional notes: 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.

BashEnvironment Variablesio redirection
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.