Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it optional to use ‘$’ character before a variable name in bash?

Tags:

bash

Could someone please explain why this works, specifically the fact that I am not using ‘$’ character before the names of the variables inside the if statement? I have searched the Bash Reference Manual, but could not find an explanation.

#!/usr/bin/env bash

one="$1"
two="$2"
three="$3"
four="$4"

if [[ one -le two ]] && [[ three -ge four ]]; then
        echo "TRUE:  $one <= $two && $three >= $four"
else
        echo "FALSE: $one <= $two && $three >= $four"
fi

I have also tested it with a loop like this, which works perfectly

for x1 in {1..3}; do
for x2 in {1..3}; do
for x3 in {1..3}; do
for x4 in {1..3}; do ./test $x1 $x2 $x3 $x4;
done; done; done; done | sort
like image 292
corisco Avatar asked Nov 05 '21 20:11

corisco


People also ask

How do you use variables in bash?

You can use variables as in any programming languages. There are no data types. A variable in bash can contain a number, a character, a string of characters. You have no need to declare a variable, just assigning a value to its reference will create it.

How do you check if a variable is defined or not in shell script?

To find out if a bash variable is defined: Return true if a bash variable is unset or set to the empty string: if [ -z ${my_variable+x} ]; Also try: [ -z ${my_bash_var+y} ] && echo "\$my_bash_var not defined"

How do you declare a string variable in bash?

To initialize a string, you directly start with the name of the variable followed by the assignment operator(=) and the actual value of the string enclosed in single or double quotes. Output: This simple example initializes a string and prints the value using the “$” operator.

How do I know if shell variable is set?

To check if a variable is set in Bash Scripting, use-v var or-z ${var} as an expression with if command.


2 Answers

Dollar signs are optional inside an arithmetic context. This is any context where a value is going to be interpreted as a number.

  • $(( ... )) creates an arithmetic context in all POSIX shells
  • (( ... )) creates an arithmetic context in bash, including in for ((expr1; expr2; expr3)).
  • let creates an arithmetic context in shells (like bash) that support that ancient, pre-POSIX, nonstandard syntax.
  • In ${array[idx]} or array[idx]=value, idx is evaluated as arithmetic as long as the array has not been declared to be associative.
  • In [[ value1 -le value2 ]], because -le is an arithmetic operator (it only does numeric comparisons, not string comparisons), both value1 and value2 are parsed as arithmetic. This is also true for -eq, -ne, -lt, -gt and -ge.
  • In ${string:start:len}, both start and len are arithmetic contexts.
  • When declare -i variable has declared a variable to have a numeric type, and variable=value is subsequently run, value is an arithmetic context.

Note that this is not true for arithmetic comparisons inside test or [ commands; test (whether or not called under the name [) acts like a regular shell command rather than special syntax (despite having a built-in implementation as a performance optimization).

like image 123
Charles Duffy Avatar answered Nov 11 '22 01:11

Charles Duffy


In the description of Bash Conditional Expressions the description of the arithmetic comparison operators (-lt, -gt, etc.) says:

When used with the [[ command, Arg1 and Arg2 are evaluated as arithmetic expressions (see Shell Arithmetic).

And when you follow that link it says:

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

And the description of Arithmetic Expansion -- $((expression)) says:

All tokens in the expression undergo parameter and variable expansion, command substitution, and quote removal. ... The evaluation is performed according to the rules listed below (see Shell Arithmetic).

like image 26
Barmar Avatar answered Nov 10 '22 23:11

Barmar