Master Shell Variables: From Basics to Advanced Tricks
This article introduces the fundamentals of shell variables, covering local and environment variables, naming rules, assignment syntax, common reference methods, numeric operations, arrays (including multidimensional techniques using eval), and special parameters, providing practical examples and essential commands for effective Bash scripting.
1. Shell Variable Basics
Shell variables are weak; by default they store strings and the shell does not interpret their meaning.
To perform arithmetic you must use commands such as let, declare, expr, or double parentheses.
Shell variables are of two types: local variables and environment variables.
Local variables are only available in the shell where they are created; environment variables are inherited by any child processes.
Variable names must start with a letter or underscore; subsequent characters may be letters, digits, or underscores. Names are case‑sensitive. No spaces are allowed around the equals sign when assigning.
Use set to list all variables, unset var to remove a variable, and readonly var to make it read‑only.
Common reference forms are illustrated below:
2. Environment Variables
Define with var=value and export with export var. During initialization the shell runs scripts such as profile that set many environment variables, which are then passed to child processes.
Use env to view the current environment. Common variables include: _: last argument of the previous command BASH: full path of the Bash executable CDPATH: colon‑separated search path for
cd EDITOR: path to the preferred editor ENV: file executed for each new Bash shell (usually .bashrc) EUID: effective user ID GROUPS: groups the current user belongs to HISTFILE and HISTSIZE: history file and its size HOME: home directory IFS: internal field separator LANG: locale setting OLDPWD: previous working directory PATH: command search path PPID: parent process ID PS1, PS2, PS3, PS4: prompt strings PWD: current directory RANDOM: random integer REPLY: default variable for
read SHELL: path of the current shell SHELLOPTS: enabled shell options UID: user ID
3. Numeric Variables
By default variables are treated as strings:
age=22
age=${age}+1
echo ${age} # prints 22+1, not 23Use let or declare with -i to perform arithmetic:
let age=${age}+1
declare -i age=224. Arrays
Arrays can be created and accessed as follows:
array[0]=0
array[1]=1
array[2]=2
# or
array=(0 1 2)Access elements with ${array[$i]}, all elements with ${array[*]}, length with ${#array[*]}, and slices with ${array[*]:0:2}.
For multidimensional arrays, eval can be used to simulate them. Example:
# simulate a 2‑D array using eval
# (actual implementation omitted for brevity)5. Special Variables
Common special parameters used in Bash scripts: $0: script name $1 … ${10}: positional parameters $#: number of arguments $*: all arguments as a single string $@: all arguments as separate strings $?: exit status of the last command $$: PID of the current shell $!: PID of the most recent background job $-: current shell options $_: last argument of the previous command
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
