Linux

Linux Shell Variables: A Comprehensive Usage Guide

This guide thoroughly explains what Linux shell variables are, how to create, modify, and use them in the command line and scripts. You'll learn to work with user and system variables, understand their scope, and apply them for task automation.

Updated at February 17, 2026
15-20 min
Easy
FixPedia Team
Применимо к:Bash 4.0+Zsh 5.0+Ubuntu 20.04+CentOS 8+

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: env or printenv
  • Delete a variable: unset VARIABLE_NAME
    unset 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:

VariableValueExample Usage
$?Exit code of the last executed command (0 = success)if [ $? -eq 0 ]; then echo "OK"; fi
$#Number of arguments passed to the script/functionInside a script: echo "Arguments: $#"
$@, $*All arguments passed to the script/functionfor arg in "$@"; do ...; done
$0Name of the script or shell itselfecho "Script launched: $0"
$$PID (process ID) of the current shellecho "My PID: $$"
$!PID of the last background processsleep 10 & echo $!
$HOMEHome directory of the current usercd $HOME
$PWDCurrent working directoryecho "You are in: $PWD"
$PATHList of directories where the system searches for executable filesecho $PATH

Verification

  1. Create a variable: TEST_VAR="success".
  2. Export it: export TEST_VAR.
  3. Launch a child process (e.g., a new sub-terminal or script) that outputs echo $TEST_VAR. You should see success.
  4. Delete the variable: unset TEST_VAR.
  5. Repeat step 3 — the output should be empty.

Common Issues

  • command not found error during assignment. Cause: spaces around =. Solution: VAR=value, not VAR = value.
  • Variable not visible in a script. Cause: you did not export it (export). Solution: use export VAR before 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 with echo $NAME or env | grep NAME.
  • Problems with spaces and special characters. Cause: the value is not enclosed in quotes. Solution: always use VAR="value with space" or VAR='value with $specialchar' (single quotes disable substitution).
  • readonly variable when trying to change. Cause: the variable was declared as readonly. Solution: create a new variable with a different name or avoid readonly for variables you need to change.

F.A.Q.

How to temporarily add a variable for a single command?
What's the difference between `export` and regular assignment?
How to make a variable read-only?
Where are system variables visible to all users stored?

Hints

Declaring and assigning a value to a variable
Reading a variable's value
Exporting a variable to child processes
Removing a variable
Working with environment variables
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community