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.
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.
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.
System Variables
Used for parameter and return‑value checks in scripts, such as $? for the exit status of the last command.
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.
Detecting Interactive Shells
case "$-" in
*i*) echo "This shell is interactive" ;;
*) echo "This shell is not interactive" ;;
esacConditional 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
Operators must be surrounded by spaces so the shell treats them as separate arguments.
Numeric Tests
File Tests
Logical Operators
Combine tests with && (AND) and || (OR) to build complex conditions.
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.
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.)
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.
