Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Operations 5 min read

20 Essential Shell Functions to Supercharge Your Scripts

This article presents twenty practical Bash functions covering file checks, directory handling, date calculations, colored output, service management, and more, providing ready-to-use snippets that streamline common shell scripting tasks for system administrators and developers.

In shell scripts you can define various functions to perform different tasks. Below are 20 commonly used custom functions covering file operations, text processing, system monitoring, and more.

Check if a file exists

Check if a directory exists

Get file size

List all files in a directory

Check if a command exists

Get current date

Get current time

Calculate difference between two dates

Print colored text

Delay execution

Check if running as root

Get IP address

Generate a random string

Check if a port is open

Restart a service

Check service status

Get the most recently modified file

Count files in a directory

Empty a file's content

Send an email

Note that some functions may need adjustments for your environment; for example, the email function relies on the mail command and the service functions require systemctl on systemd‑based systems.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

shell-scriptingSystem Automationbash functions

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion