Bash comparisions Flashcards

1
Q

Syntax for comparing variable with a value

A

Always use double quotations with the variable.

e.g. “$myvariable” -eq “this is a string”

This invariably avoids errors. Whereas sometimes quotations are necessary and omitting them generates errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Zero LENGTH string

Non zero LENGTH string

A
  • z “$mystring”
  • nz “$mystring”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

String equality

String inequality

Numeric equality

Numeric inequality

Numeric less than

Numeric less or equal

Numeric greater than

Numeric greater or equal

A

“abc” =“$mystring”

“abc” !=“$mystring”

3 -eq“$mystring”

3 -ne“$mystring”

3 -lt“$mystring”

3 -le“$mystring”

3 -gt“$mystring”

3 -ge“$mystring”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Exists and is a regular file

Exists and is a directory

1st file newer than second file

1st file older than second file

A

-f“$myfile”

-d“$mydir”

“$myfile” -ot./bashrc

“$myfile” -nt./bashrc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly