Introduction / Why This Is Needed
Shell variables are a fundamental mechanism of the Linux terminal, allowing you to store and reuse data: file paths, program settings, command outputs. They form the basis for writing effective Bash/Zsh scripts, configuring the development environment, and automating routine tasks. Understanding how to work with variables dramatically improves command-line efficiency.
Requirements / Preparation
- Access to a Linux terminal (any distribution: Ubuntu, Fedora, Debian, Arch, etc.).
- Basic familiarity with the command line.
- The default shell in use should be Bash (recommended) or Zsh. You can check with
echo $SHELL. - Permissions to execute standard commands (
echo,export,unset,set,env).
Step 1: Declaring and Assigning a Value to a Variable
Creating a variable is done with a simple assignment. Extremely important: there must be no spaces around the equals sign =. A variable name can contain letters, numbers, and the underscore character (_), but it cannot start with a digit.
# Simple declaration
GREETING="Hello, world!"
# You can assign the result of a command (using command substitution)
CURRENT_DATE=$(date +%Y-%m-%d)
USER_HOME=$(echo $HOME) # $HOME is an existing system variable
# Example with a number (numbers in the shell are also strings, but there is special syntax for arithmetic)
COUNTER=10
💡 Tip: Always enclose the value in quotes (
"), especially if it contains spaces or special characters. This prevents interpretation errors.
Step 2: Reading and Using the Value
To get a variable's value, prefix its name with a dollar sign $. For complex cases (e.g., to separate the variable name from subsequent text), use curly braces ${}.
# Simple reading
echo $GREETING
# Output: Hello, world!
# Using in a string with ${}
echo "Today: ${CURRENT_DATE}, user: $USER"
# Output: Today: 2026-02-17, user: fixpedia
# Concatenation (string addition)
PATH_SUFFIX="/myapp/bin"
FULL_PATH="/usr/local"$PATH_SUFFIX
echo $FULL_PATH
# Output: /usr/local/myapp/bin
# Using in a command
LIST_FILE="filelist.txt"
wc -l $LIST_FILE
Step 3: Exporting Variables (Making Them Visible to Other Programs)
By default, a variable exists only in the current shell. To make it an environment variable and make it available to any programs and scripts launched from that shell, you need to export it.
# 1. Declare
MY_APP_CONFIG="/etc/myapp/config.conf"
# 2. Export
export MY_APP_CONFIG
# Verify it's an environment variable
env | grep MY_APP_CONFIG
# Output: MY_APP_CONFIG=/etc/myapp/config.conf
# Now any script or program launched from this terminal
# will be able to read $MY_APP_CONFIG.
You can declare and export a variable in a single command:
export API_KEY="secret123"
Step 4: Special Types of Variables
4.1. Read-Only (readonly)
Once declared, the value of such a variable cannot be changed or deleted. Useful for constants.
readonly APP_VERSION="2.1.0"
APP_VERSION="3.0.0" # Error! bash: APP_VERSION: readonly variable
4.2. Local Variables in Functions (local)
Inside a function (or script), a variable declared as local is visible only within that function/script and does not overwrite a global variable with the same name.
GLOBAL_VAR="I am global"
my_function() {
local GLOBAL_VAR="I am local"
echo "Inside function: $GLOBAL_VAR"
}
my_function
echo "Outside function: $GLOBAL_VAR"
# Output:
# Inside function: I am local
# Outside function: I am global
Step 5: Viewing and Deleting Variables
- View all variables (local and environment):
set - View only environment variables:
envorprintenv - Delete a variable:
unset VARIABLE_NAMEunset GREETING echo $GREETING # Outputs an empty string
Step 6: Working with Built-in and Special Variables
The shell provides many useful built-in variables. Here are the key ones:
| Variable | Value | Example Usage |
|---|---|---|
$? | Exit code of the last executed command (0 = success) | if [ $? -eq 0 ]; then echo "OK"; fi |
$# | Number of arguments passed to the script/function | Inside a script: echo "Arguments: $#" |
$@, $* | All arguments passed to the script/function | for arg in "$@"; do ...; done |
$0 | Name of the script or shell itself | echo "Script launched: $0" |
$$ | PID (process ID) of the current shell | echo "My PID: $$" |
$! | PID of the last background process | sleep 10 & echo $! |
$HOME | Home directory of the current user | cd $HOME |
$PWD | Current working directory | echo "You are in: $PWD" |
$PATH | List of directories where the system searches for executable files | echo $PATH |
Verification
- Create a variable:
TEST_VAR="success". - Export it:
export TEST_VAR. - Launch a child process (e.g., a new sub-terminal or script) that outputs
echo $TEST_VAR. You should seesuccess. - Delete the variable:
unset TEST_VAR. - Repeat step 3 — the output should be empty.
Common Issues
command not founderror during assignment. Cause: spaces around=. Solution:VAR=value, notVAR = value.- Variable not visible in a script. Cause: you did not export it (
export). Solution: useexport VARbefore running the script or declare it inside the script. - Empty value when reading. Cause: the variable was not declared, was deleted (
unset), or its name was typed incorrectly (case-sensitive!). Solution: check the name withecho $NAMEorenv | grep NAME. - Problems with spaces and special characters. Cause: the value is not enclosed in quotes. Solution: always use
VAR="value with space"orVAR='value with $specialchar'(single quotes disable substitution). readonly variablewhen trying to change. Cause: the variable was declared asreadonly. Solution: create a new variable with a different name or avoidreadonlyfor variables you need to change.