30 KiB
Bash Utilites
Bash library which provides utility functions and helpers for functional programming in Bash.Table of Contents
- Array
- Date
- date::now()
- date::epoc()
- date::add_days_from()
- date::add_months_from()
- date::add_years_from()
- date::add_weeks_from()
- date::add_hours_from()
- date::add_minutes_from()
- date::add_seconds_from()
- date::add_days()
- date::add_months()
- date::add_years()
- date::add_weeks()
- date::add_hours()
- date::add_minutes()
- date::add_seconds()
- date::sub_days_from()
- date::sub_months_from()
- date::sub_years_from()
- date::sub_weeks_from()
- date::sub_hours_from()
- date::sub_minutes_from()
- date::sub_seconds_from()
- date::sub_days()
- date::sub_months()
- date::sub_years()
- date::sub_weeks()
- date::sub_hours()
- date::sub_minutes()
- date::sub_seconds()
- date::format()
- date::human_readable_seconds()
- File
- Interaction
- Miscellaneous
- String
- Variable
- Inspired By
- License
Array
Functions for array operations and manipulations.
array::contains()
Check if item exists in the given array.
Example
array=("a" "b" "c")
array::contains "c" ${array[@]}
#Output
0
Arguments
- $1 (mixed): Item to search (needle).
- $2 (array): array to be searched (haystack).
Exit codes
- 0: If successful.
- 1: If no match found in the array.
- 2: Function missing arguments.
array::dedupe()
Remove duplicate items from the array.
Example
array=("a" "b" "a" "c")
printf "%s" "$(array::dedupe ${array[@]})"
#Output
a b c
Arguments
- $1 (array): Array to be deduped.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- Deduplicated array.
array::is_empty()
Check if a given array is empty.
Example
array=("a" "b" "c" "d")
array::is_empty "${array[@]}"
Arguments
- $1 (array): Array to be checked.
Exit codes
- 0: If the given array is empty.
- 2: If the given array is not empty.
array::join()
Join array elements with a string.
Example
array=("a" "b" "c" "d")
printf "%s" "$(array::join "," "${array[@]}")"
#Output
a,b,c,d
printf "%s" "$(array::join "" "${array[@]}")"
#Output
abcd
Arguments
- $1 (string): String to join the array elements (glue).
- $2 (array): array to be joined with glue string.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- String containing a string representation of all the array elements in the same order,with the glue string between each element.
array::reverse()
Return an array with elements in reverse order.
Example
array=(1 2 3 4 5)
printf "%s" "$(array::reverse "${array[@]}")"
#Output
5 4 3 2 1
Arguments
- $1 (array): The input array.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- The reversed array.
array::random_element()
Returns a random item from the array.
Example
array=("a" "b" "c" "d")
printf "%s\n" "$(array::random_element "${array[@]}")"
#Output
c
Arguments
- $1 (array): The input array.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- Random item out of the array.
Date
Functions for manipulating dates.
date::now()
Get current time in unix timestamp.
Example
echo "$(date::now)"
#Output
1591554426
Function has no arguments.
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- current timestamp.
date::epoc()
convert datetime string to unix timestamp.
Example
echo "$(date::epoc "2020-07-07 18:38")"
#Output
1594143480
Arguments
- $1 (string): date time in any format.
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp for specified datetime.
date::add_days_from()
Add number of days from specified timestamp. If number of days not specified then it defaults to 1 day.
Example
echo "$(date::add_days_from "1594143480")"
#Output
1594229880
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of days (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::add_months_from()
Add number of months from specified timestamp. If number of months not specified then it defaults to 1 month.
Example
echo "$(date::add_months_from "1594143480")"
#Output
1596821880
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of months (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::add_years_from()
Add number of years from specified timestamp. If number of years not specified then it defaults to 1 year.
Example
echo "$(date::add_years_from "1594143480")"
#Output
1625679480
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of years (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::add_weeks_from()
Add number of weeks from specified timestamp. If number of weeks not specified then it defaults to 1 week.
Example
echo "$(date::add_weeks_from "1594143480")"
#Output
1594748280
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of weeks (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::add_hours_from()
Add number of hours from specified timestamp. If number of hours not specified then it defaults to 1 hour.
Example
echo "$(date::add_hours_from "1594143480")"
#Output
1594147080
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of hours (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::add_minutes_from()
Add number of minutes from specified timestamp. If number of minutes not specified then it defaults to 1 minute.
Example
echo "$(date::add_minutes_from "1594143480")"
#Output
1594143540
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of minutes (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::add_seconds_from()
Add number of seconds from specified timestamp. If number of seconds not specified then it defaults to 1 second.
Example
echo "$(date::add_seconds_from "1594143480")"
#Output
1594143481
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of seconds (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::add_days()
Add number of days from current day timestamp. If number of days not specified then it defaults to 1 day.
Example
echo "$(date::add_days "1")"
#Output
1591640826
Arguments
- $1 (int): number of days (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::add_months()
Add number of months from current day timestamp. If number of months not specified then it defaults to 1 month.
Example
echo "$(date::add_months "1")"
#Output
1594146426
Arguments
- $1 (int): number of months (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::add_years()
Add number of years from current day timestamp. If number of years not specified then it defaults to 1 year.
Example
echo "$(date::add_years "1")"
#Output
1623090426
Arguments
- $1 (int): number of years (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::add_weeks()
Add number of weeks from current day timestamp. If number of weeks not specified then it defaults to 1 year.
Example
echo "$(date::add_weeks "1")"
#Output
1592159226
Arguments
- $1 (int): number of weeks (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::add_hours()
Add number of hours from current day timestamp. If number of hours not specified then it defaults to 1 hour.
Example
echo "$(date::add_hours "1")"
#Output
1591558026
Arguments
- $1 (int): number of hours (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::add_minutes()
Add number of minutes from current day timestamp. If number of minutes not specified then it defaults to 1 minute.
Example
echo "$(date::add_minutes "1")"
#Output
1591554486
Arguments
- $2 (int): number of minutes (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::add_seconds()
Add number of seconds from current day timestamp. If number of seconds not specified then it defaults to 1 second.
Example
echo "$(date::add_seconds "1")"
#Output
1591554427
Arguments
- $2 (int): number of seconds (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::sub_days_from()
Subtract number of days from specified timestamp. If number of days not specified then it defaults to 1 day.
Example
echo "$(date::sub_days_from "1594143480")"
#Output
1594057080
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of days (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::sub_months_from()
Subtract number of months from specified timestamp. If number of months not specified then it defaults to 1 month.
Example
echo "$(date::sub_months_from "1594143480")"
#Output
1591551480
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of months (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::sub_years_from()
Subtract number of years from specified timestamp. If number of years not specified then it defaults to 1 year.
Example
echo "$(date::sub_years_from "1594143480")"
#Output
1562521080
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of years (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::sub_weeks_from()
Subtract number of weeks from specified timestamp. If number of weeks not specified then it defaults to 1 week.
Example
echo "$(date::sub_weeks_from "1594143480")"
#Output
1593538680
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of weeks (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::sub_hours_from()
Subtract number of hours from specified timestamp. If number of hours not specified then it defaults to 1 hour.
Example
echo "$(date::sub_hours_from "1594143480")"
#Output
1594139880
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of hours (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::sub_minutes_from()
Subtract number of minutes from specified timestamp. If number of minutes not specified then it defaults to 1 minute.
Example
echo "$(date::sub_minutes_from "1594143480")"
#Output
1594143420
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of minutes (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::sub_seconds_from()
Subtract number of seconds from specified timestamp. If number of seconds not specified then it defaults to 1 second.
Example
echo "$(date::sub_seconds_from "1594143480")"
#Output
1594143479
Arguments
- $1 (int): unix timestamp.
- $2 (int): number of seconds (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
- 2: Function missing arguments.
Output on stdout
- timestamp.
date::sub_days()
Subtract number of days from current day timestamp. If number of days not specified then it defaults to 1 day.
Example
echo "$(date::sub_days "1")"
#Output
1588876026
Arguments
- $1 (int): number of days (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::sub_months()
Subtract number of months from current day timestamp. If number of months not specified then it defaults to 1 month.
Example
echo "$(date::sub_months "1")"
#Output
1559932026
Arguments
- $1 (int): number of months (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::sub_years()
Subtract number of years from current day timestamp. If number of years not specified then it defaults to 1 year.
Example
echo "$(date::sub_years "1")"
#Output
1591468026
Arguments
- $1 (int): number of years (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::sub_weeks()
Subtract number of weeks from current day timestamp. If number of weeks not specified then it defaults to 1 week.
Example
echo "$(date::sub_weeks "1")"
#Output
1590949626
Arguments
- $1 (int): number of weeks (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::sub_hours()
Subtract number of hours from current day timestamp. If number of hours not specified then it defaults to 1 hour.
Example
echo "$(date::sub_hours "1")"
#Output
1591550826
Arguments
- $1 (int): number of hours (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::sub_minutes()
Subtract number of minutes from current day timestamp. If number of minutes not specified then it defaults to 1 minute.
Example
echo "$(date::sub_minutes "1")"
#Output
1591554366
Arguments
- $1 (int): number of minutes (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::sub_seconds()
Subtract number of seconds from current day timestamp. If number of seconds not specified then it defaults to 1 second.
Example
echo "$(date::sub_seconds "1")"
#Output
1591554425
Arguments
- $1 (int): number of seconds (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate timestamp.
Output on stdout
- timestamp.
date::format()
Format unix timestamp to human readable format. If format string is not specified then it defaults to "yyyy-mm-dd hh:mm:ss" format.
Example
echo echo "$(date::format "1594143480")"
#Output
2020-07-07 18:38:00
Arguments
- $1 (int): unix timestamp.
- $2 (string): format control characters based on
datecommand (optional).
Exit codes
- 0: If successful.
- 1: If unable to generate time string.
- 2: Function missing arguments.
Output on stdout
- formatted time string.
date::human_readable_seconds()
Format seconds to human readable format.
Example
echo "$(date::human_readable_seconds "356786")"
#Output
4 days 3 hours 6 minute(s) and 26 seconds
Arguments
- $1 (int): number of seconds.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- formatted time string.
File
Functions for handling files.
file::make_temp_file()
Create temporary file. Function creates temporary file with random name. The temporary file will be deleted when script finishes.
Example
echo "$(file::make_temp_file)"
#Output
tmp.vgftzy
Function has no arguments.
Exit codes
- 0: If successful.
- 1: If failed to create temp file.
Output on stdout
- file name of temporary file created.
file::name()
Get only the filename from string path.
Example
echo "$(file::name "/path/to/test.md")"
#Output
test.md
Arguments
- $1 (string): path.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- name of the file with extension.
file::basename()
Get the basename of file from file name.
Example
echo "$(file::basename "/path/to/test.md")"
#Output
test
Arguments
- $1 (string): path.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- basename of the file.
file::extension()
Get the extension of file from file name.
Example
echo "$(file::extension "/path/to/test.md")"
#Output
md
Arguments
- $1 (string): path.
Exit codes
- 0: If successful.
- 1: If no extension is found in the filename.
- 2: Function missing arguments.
Output on stdout
- extension of the file.
file::dirname()
Get directory name from file path.
Example
echo "$(file::dirname "/path/to/test.md")"
#Output
/path/to
Arguments
- $1 (string): path.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- directory path.
file::full_path()
Get absolute path of file or directory.
Example
file::full_path "../path/to/file.md"
#Output
/home/labbots/docs/path/to/file.md
Arguments
- $1 (string): relative or absolute path to file/direcotry.
Exit codes
- 0: If successful.
- 1: If file/directory does not exist.
- 2: Function missing arguments.
Output on stdout
- Absolute path to file/directory.
file::mime_type()
Get mime type of provided input.
Example
file::mime_type "../src/file.sh"
#Output
application/x-shellscript
Arguments
- $1 (string): relative or absolute path to file/direcotry.
Exit codes
- 0: If successful.
- 1: If file/directory does not exist.
- 2: Function missing arguments.
- 3: If file or mimetype command not found in system.
Output on stdout
- mime type of file/directory.
Interaction
Functions to enable interaction with the user.
interaction::prompt_yes_no()
Prompt yes or no question to the user.
Example
interaction::prompt_yes_no "Are you sure to proceed" "yes"
#Output
Are you sure to proceed (y/n)? [y]
Arguments
- $1 (string): The question to be prompted to the user.
- $2 (string): default answer [yes/no] (optional).
Exit codes
- 0: If user responds with yes.
- 1: If user responds with no.
- 2: Function missing arguments.
Output on stdout
- question to be prompted to the user.
interaction::prompt_response()
Prompt question to the user.
Example
interaction::prompt_response "Choose directory to install" "/home/path"
#Output
Choose directory to install? [/home/path]
Arguments
- $1 (string): The question to be prompted to the user.
- $2 (string): default answer (optional).
Exit codes
- 0: If user responds with yes.
- 2: Function missing arguments.
Output on stdout
- question to be prompted to the user.
Miscellaneous
Set of miscellaneous helper functions.
misc::check_internet_connection()
Check if internet connection is available.
Example
misc::check_internet_connection
Function has no arguments.
Exit codes
- 0: If script can connect to internet.
- 1: If script cannot access internet.
String
Functions for string operations and manipulations.
string::trim()
Strip whitespace from the beginning and end of a string.
Example
echo "$(string::trim " Hello World! ")"
#Output
Hello World!
Arguments
- $1 (string): The string to be trimmed.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- The trimmed string.
string::split()
Split a string to array by a delimiter.
Example
printf "%s" "$(string::split "Hello!World" "!")"
#Output
Hello
World
Arguments
- $1 (string): The input string.
- $2 (string): The delimiter string.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- Returns an array of strings created by splitting the string parameter by the delimiter.
string::lstrip()
Strip characters from the beginning of a string.
Example
echo "$(string::lstrip "Hello World!" "He")"
#Output
llo World!
Arguments
- $1 (string): The input string.
- $2 (string): The characters you want to strip.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- Returns the modified string.
string::rstrip()
Strip characters from the end of a string.
Example
echo "$(string::rstrip "Hello World!" "d!")"
#Output
Hello Worl
Arguments
- $1 (string): The input string.
- $2 (string): The characters you want to strip.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- Returns the modified string.
string::to_lower()
Make a string lowercase.
Example
echo "$(string::to_lower "HellO")"
#Output
hello
Arguments
- $1 (string): The input string.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- Returns the lowercased string.
string::to_upper()
Make a string all uppercase.
Example
echo "$(string::to_upper "HellO")"
#Output
HELLO
Arguments
- $1 (string): The input string.
Exit codes
- 0: If successful.
- 2: Function missing arguments.
Output on stdout
- Returns the uppercased string.
string::contains()
Check whether the search string exists within the input string.
Example
string::contains "Hello World!" "lo"
Arguments
- $1 (string): The input string.
- $2 (string): The search key.
Exit codes
- 0: If match found.
- 1: If no match found.
- 2: Function missing arguments.
string::starts_with()
Check whether the input string starts with key string.
Example
string::starts_with "Hello World!" "He"
Arguments
- $1 (string): The input string.
- $2 (string): The search key.
Exit codes
- 0: If match found.
- 1: If no match found.
- 2: Function missing arguments.
string::ends_with()
Check whether the input string ends with key string.
Example
string::ends_with "Hello World!" "d!"
Arguments
- $1 (string): The input string.
- $2 (string): The search key.
Exit codes
- 0: If match found.
- 1: If no match found.
- 2: Function missing arguments.
string::regex()
Check whether the input string matches the given regex.
Example
string::regex "HELLO" "^[A-Z]*$"
Arguments
- $1 (string): The input string.
- $2 (string): The search key.
Exit codes
- 0: If match found.
- 1: If no match found.
- 2: Function missing arguments.
Variable
Functions for handling variables.
variable::is_array()
Check if given variable is array.
Example
array=("a" "b" "c")
variable::is_array "${array[@]}"
#Output
0
Arguments
- $1 (mixed): Value of variable to check.
Exit codes
- 0: If input is array.
- 1: If input is not an array.
variable::is_numeric()
Check if given variable is a number.
Example
variable::is_numeric "1234"
#Output
0
Arguments
- $1 (mixed): Value of variable to check.
Exit codes
- 0: If input is number.
- 1: If input is not a number.
variable::is_int()
Check if given variable is an integer.
Example
variable::is_int "+1234"
#Output
0
Arguments
- $1 (mixed): Value of variable to check.
Exit codes
- 0: If input is an integer.
- 1: If input is not an integer.
variable::is_float()
Check if given variable is a float.
Example
variable::is_float "+1234.0"
#Output
0
Arguments
- $1 (mixed): Value of variable to check.
Exit codes
- 0: If input is a float.
- 1: If input is not a float.
variable::is_bool()
Check if given variable is a boolean.
Example
variable::is_bool "true"
#Output
0
Arguments
- $1 (mixed): Value of variable to check.
Exit codes
- 0: If input is a boolean.
- 1: If input is not a boolean.
variable::is_true()
Check if given variable is a true.
Example
variable::is_true "true"
#Output
0
Arguments
- $1 (mixed): Value of variable to check.
Exit codes
- 0: If input is true.
- 1: If input is not true.
variable::is_false()
Check if given variable is false.
Example
variable::is_false "false"
#Output
0
Arguments
- $1 (mixed): Value of variable to check.
Exit codes
- 0: If input is false.
- 1: If input is not false.
Inspired By
- Bash Bible - A collection of pure bash alternatives to external processes.