Leandro Toshio Takeda
Leandro Toshio Takeda

Reputation: 1639

Assign new value to variable fails

why this do not work:

if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
                $_TYPE=$(echo "Outbound");
fi

or

if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
                $_TYPE=echo "Outbound";
fi

or

if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
                $_TYPE="Outbound";
fi

I'm receiving this error: line 251: 2=Outbound: command not found

Upvotes: 0

Views: 57

Answers (2)

Junior Dussouillez
Junior Dussouillez

Reputation: 2397

$ is used to access the value, but if you have to assign a value, the syntax is :

_TYPE="newValue"

Upvotes: 1

ruakh
ruakh

Reputation: 183270

In POSIX shells such as Bash, $ is not part of the variable-name, it's just the notation for expanding the variable (to obtain its value); so, for example, echo "$_TYPE" prints the value of the variable _TYPE. You don't use the $ when you're assigning to the variable. So you just need:

if [[ "$_TYPE" = 1 || "$_TYPE" = 2 ]] ; then
    _TYPE=Outbound
fi

Upvotes: 3

Related Questions