bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Bash/Scripting
Bash•Scripting

Bash Operators

Understanding Bash Operators

This section provides an overview of operators used in Bash scripting, including comparison, string, arithmetic, logical, and file test operators.

Comparison Operators

  • -eq : Equal to
  • -ne : Not equal to
  • -lt : Less than
  • -le : Less than or equal to
  • -gt : Greater than
  • -ge : Greater than or equal to

String Comparison Operators

  • = : Equal to
  • != : Not equal to
  • < : Less than, in ASCII alphabetical order
  • > : Greater than, in ASCII alphabetical order

Arithmetic Operators

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (remainder of division)
  • For exponentiation, use external tools like bc or awk .

Logical Operators

  • && : Logical AND
-: Logical OR
  • ! : Logical NOT

File Test Operators

  • -e : Checks if a file exists
  • -d : Checks if a directory exists
  • -f : Checks if a file is a regular file
  • -s : Checks if a file is not empty

Previous

Bash Data Types

Next

Bash If...Else

This chapter

Overview
10

Lessons

20m

Read time

1. Basic Bash Syntax2. Bash Script3. Bash Variables4. Bash Data Types5. Bash Operators6. Bash If...Else7. Bash Loops8. Bash Functions9. Bash Arrays10. Bash crontab Command - Schedule Tasks

On this page

Understanding Bash OperatorsComparison OperatorsString Comparison OperatorsArithmetic OperatorsLogical OperatorsFile Test Operators