DEV Community

Onelinerhub
Onelinerhub

Posted on

3 1

5 useful bash expressions to use with IF

1. How to check if specific bash command is available:

if command -v CMD &> /dev/null; then echo "it exists"; fi
Enter fullscreen mode Exit fullscreen mode

Image description

Edit or save this code on github.

2. How to check if specific file exists:

if [ -f /tmp/file ]; then echo 1; fi
Enter fullscreen mode Exit fullscreen mode

Image description

Edit or save this code on github.

3. How to check if scecific directory exists:

if [ -d /tmp ]; then echo 1; fi
Enter fullscreen mode Exit fullscreen mode

Image description

Edit or save this code on github.

4. How to check if variable is empty

if [ -z "$variable" ]; then echo "\$variable is empty"; fi
Enter fullscreen mode Exit fullscreen mode

Image description

Edit or save this code on github.

5. How to check if variable is not empty

if [ ! -z "$variable" ]; then echo "\$variable is not empty"; fi
Enter fullscreen mode Exit fullscreen mode

Edit or save this code on github.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs