Syntax

Bash If Statement Syntax

In Bash, if statement: runs code when a test is true — if [[ -f file.txt ]]; then echo "exists" fi

Quick Answer

Syntax
if [[ -f file.txt ]]; then echo "exists" fi
BashSyntax

If Statement

Code example
if [[ -f file.txt ]]; then echo "exists" fi

Runs code when a test is true.

Learn More

How to if statement in Bash

  1. Open your Bash file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Bash If Statement Syntax

Use if [[ -f file.txt ]]; then echo "exists" fi.

Related Syntax References

Frequently asked questions

What is the Bash syntax for if statement?

Use this pattern: if [[ -f file.txt ]]; then echo "exists" fi

Can I copy this Bash code example?

Yes. Use Copy code and adapt the example to your program.

Is this Bash syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify