0

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 $?

1
  • 2
    Basically because it is a bash built-in command (compared with either the Bourne shell or the POSIX shell) and it has been decreed that variables do not need to be prefixed with $ in this one context. Commented Dec 27, 2013 at 4:27

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.