# Library Functions

Bashly comes with a set of library functions that can be added to your script by running the bashly add command.

# YAML parser

Adds the ability to read YAML files in your bash script.

$ bashly add yaml

You can then use the yaml_load function anywhere in your script:

yaml_load "settings.yml"             # print variables
yaml_load "settings.yml" "config_"   # use prefix
eval $(yaml_load "settings.yml")     # create variables in scope

YAML Example

# INI handler

Adds the ability to create, read and write INI files.

$ bashly add config

By default, these functions will work on a file named config.ini but you can change it by setting the CONFIG_FILE environment variable in your src/initialize.sh file.

You can then use thees functions anywhere in your script:

# Create a new config file.
# There is normally no need to use this function, it is used by other
# functions as needed.

# Get the value from the config file.
result=$(config_get username)

# Add or update a key=value pair in the config.
config_set username bob

# Delete a key from the config.
config_del username

# Show the config file.

# Return an array of the keys in the config file
for k in $(config_keys); do
  echo "- $k = $(config_get "$k")";

# Returns true if the specified key exists in the config file
if config_has_key "key" ; then
  echo "key exists"

Config Example

# Color output

Adds functions for printing colored strings.

$ bashly add colors

Then, anywhere in your script:

echo "before $(red this is red) after"
echo "before $(green_bold this is green_bold) after"

See the generated script in src/lib/colors.sh for the full list of colors.

Colors Example

# Auto-update

Files added by the bashly add * commands can be automatically updated to their original state by running

bashly generate --upgrade

The --upgrade flag will scan all the files in the src/lib directory for a special magic comment in this format:

[@bashly-upgrade <library>]

When found, and assuming the path of the file matches the one in the library, this file will be updated.

You are encouraged to modify the generated library functions to your liking, but if you do so, remember to remove this magic comment to prevent accidentally overriding it in future runs of bashly generate --upgrade.