Fundamentals 9 min read

Master Linux Shell: Types, Variables, Config Files, and Conditional Tests

This guide explains the fundamentals of Linux shells—including common shell types, variable classifications, environment and user-defined variables, the order in which configuration files are loaded for interactive and login sessions, and how to write reliable conditional tests in shell scripts.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Shell: Types, Variables, Config Files, and Conditional Tests

Shell Types

Linux provides several shells such as sh , bash , and zsh . Bash is the default on most distributions and is the most common for scripting.

Shell history diagram
Shell history diagram

The current shell can be queried with echo $0 or by inspecting the $SHELL environment variable.

Variable Classification

Shell variables fall into three categories:

Environment variables (e.g., JAVA_HOME) are inherited by all child processes.

System variables are used by the shell itself for internal logic and status codes.

User‑defined variables are created by the user for script logic.

Environment Variables

Define them with export VAR=value. They persist only for the current shell session unless added to a startup file such as ~/.bashrc or /etc/profile.

Export example
Export example

System Variables

Used for parameter and return‑value checks in scripts, such as $? for the exit status of the last command.

System variable usage
System variable usage

User‑Defined Variables

When assigning, avoid spaces around the = sign and use proper quoting to prevent word splitting.

Correct: NAME="value" Incorrect: NAME = "value" Variable names are case‑sensitive.

Shell Startup File Loading Order

Shell startup files are read depending on whether the shell is interactive, login, or non‑interactive.

Interactive & Login : /etc/profile → one of ~/.bash_profile, ~/.bash_login, or ~/.profile~/.bash_logout on exit.

Interactive & Non‑Login : /etc/bash.bashrc~/.bashrc.

Non‑Interactive : Configuration is taken from the environment (e.g., env output); no startup files are read.

Startup file order
Startup file order

Detecting Interactive Shells

case "$-" in
  *i*) echo "This shell is interactive" ;;
  *)   echo "This shell is not interactive" ;;
esac

Conditional Tests

Shell conditionals return 0 for true and non‑zero for false. Two common syntaxes are test expression and [ expression ] (spaces are required).

String Tests

String test examples
String test examples
Operators must be surrounded by spaces so the shell treats them as separate arguments.

Numeric Tests

Numeric test examples
Numeric test examples

File Tests

File test examples
File test examples

Logical Operators

Logical operator tests
Logical operator tests

Combine tests with && (AND) and || (OR) to build complex conditions.

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.

configurationLinuxShellbashscriptenvironment-variables
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.