Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash if statement: Can I do an assignment and comparison?

I want to do something like:

if [ CURRENT=$(stat -c %Y $STATUS_FILE) -ne $LASTUPDATE ]
    then LASTUPDATE = $CURRENT
fi

That is... I want to assign a variable and do a comparison since I'll use that variable later

like image 870
David Parks Avatar asked Jul 06 '11 03:07

David Parks


People also ask

Can you have multiple if statements in bash?

Bash else-if statement is used for multiple conditions. It is just like an addition to Bash if-else statement. In Bash elif, there can be several elif blocks with a boolean expression for each one of them. In the case of the first 'if statement', if a condition goes false, then the second 'if condition' is checked.

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.

What is the difference between and == in bash?

In short (in bash): Yes both = and == are the same (inside test constructs).

Can you assign 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.


1 Answers

Absolutely.

$ echo "$foo"

$ echo "${foo=$(echo bar)}"
bar
$ echo "$foo"
bar
like image 197
Ignacio Vazquez-Abrams Avatar answered Oct 24 '22 18:10

Ignacio Vazquez-Abrams