beakr
beakr

Reputation: 5867

Why can you use -eq in Bash conditionals without adding a '$' before a variable name?

I was running some Bash conditional scripts and discovered if I run this:

#!/bin/bash

read foo

if [[ foo -eq 1 ]]; then
  echo "A"
fi

if [[ foo -eq 2 ]]; then
  echo "B"
fi

The conditionals work fine under Bash 4.2.25 without the use of $foo. Why does this work without referencing the variable with a $?

Upvotes: 0

Views: 92

Answers (1)

Barmar
Barmar

Reputation: 781706

From the description of bash Conditional Constructs, it says that [[ expression ]] performs arithmetic expansion of the expression. If you then find the section on Shell Arithmetic it says:

Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax.

"parameter expansion syntax" refers to putting a $ before the name.

Upvotes: 3

Related Questions