Lesson 4 of 5 · Linux for DBAs

Linux Variables

How shell variables store values, how to set and use them, and the quoting rules that keep your commands and scripts working.

Beginner 8 sections 4 min read

What's a variable

A variable is a named placeholder for a value. The name stays the same, and the value can change as needed.

x = 1 Variable name stays the same Value can change

Why variables are useful

Variables let you store a value once, then reference and change it throughout your commands and scripts. Their key benefit: your logic stays the same while the values change.

x=1 sum=$((4 + x)) echo $sum x=7 sum=$((4 + x)) echo $sum 5 11 x is 1 x is 7: same logic, new result

In bash, math goes inside $(( )). Only the value of x changed between the two examples; the line that calculates sum stayed exactly the same.

Declaring a variable

Declaring (setting) a variable takes one line: the name, an equals sign, and the value.

name=value
student="kofi"       # the variable is student and its value is kofi

Variable names can use letters, numbers, and underscores, but can't start with a number. Names are case-sensitive: student and STUDENT are two different variables.

Using a variable

To use a variable's value, put a $ in front of its name. echo prints it to the screen:

$ echo $student
kofi

You can use a variable anywhere in a command, including inside a sentence:

$ student="John Doe"                  # declare the variable
$ echo "Hi, my name is $student"      # use it in a sentence
Hi, my name is John Doe

If text follows the variable name directly, wrap the name in braces so bash knows where it ends: echo "${student}_backup".

Text, numbers, and quotes

  • Quote values that contain spaces or special characters. name="John Doe" works; without the quotes, bash stops at the space and tries to run Doe as a command.
  • Numbers don't need quotes: age=18.
  • Uppercase and lowercase are kept exactly as you type them, with or without quotes. "McDonald" and "mcdonald" are always two different values.
  • Double quotes expand variables; single quotes don't. echo "Hi $student" prints Hi kofi, but echo 'Hi $student' prints Hi $student exactly as written.

Variables in action

# declare variables
name="John Doe"
age=18

# output a message
echo "Hi world, my name is $name and I am $age years old."
Hi world, my name is John Doe and I am 18 years old.

The variables every Oracle DBA uses

Variables you set with name=value only exist in your current shell. export makes a variable an environment variable, passed on to every program you start, such as SQL*Plus. Oracle's tools read three of them:

VariableTells Oracle's tools
ORACLE_SIDWhich database instance on this server to work with
ORACLE_HOMEWhere the Oracle software is installed
PATHWhere to find commands such as sqlplus; $ORACLE_HOME/bin is added to it
# this server runs two databases, SALES and HR; work on SALES
$ export ORACLE_SID=SALES
$ export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
$ export PATH=$ORACLE_HOME/bin:$PATH
$ echo $ORACLE_SID
SALES
$ sqlplus / as sysdba        # connects to SALES

Oracle provides a script, oraenv, that sets all three for you, and DBAs usually add their settings to ~/.bash_profile so they're ready at every login.

Key takeaways

  • A variable is a named placeholder for a value that can change.
  • Set it with name=value, with no spaces around the =.
  • Use it with $name; echo $name prints the value.
  • Quote text values, especially ones with spaces. Use double quotes when you want variables expanded inside.
  • Variable names and values are case-sensitive.