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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
