{"id":13587564,"url":"https://github.com/gruntwork-io/bash-commons","last_synced_at":"2025-06-10T15:31:07.626Z","repository":{"id":42789528,"uuid":"128768909","full_name":"gruntwork-io/bash-commons","owner":"gruntwork-io","description":"A collection of reusable Bash functions for handling common tasks such as logging, assertions, string manipulation, and more","archived":false,"fork":false,"pushed_at":"2024-12-03T02:43:11.000Z","size":214,"stargazers_count":773,"open_issues_count":2,"forks_count":165,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-04-07T22:35:37.967Z","etag":null,"topics":["bash","devops","linux","scripting"],"latest_commit_sha":null,"homepage":"https://gruntwork.io/","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gruntwork-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"gruntwork-io"}},"created_at":"2018-04-09T12:34:13.000Z","updated_at":"2025-03-31T03:31:33.000Z","dependencies_parsed_at":"2024-06-27T22:26:14.611Z","dependency_job_id":"be3f7b9e-5a37-47e8-a6c7-d5086df765d1","html_url":"https://github.com/gruntwork-io/bash-commons","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gruntwork-io%2Fbash-commons","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gruntwork-io%2Fbash-commons/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gruntwork-io%2Fbash-commons/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gruntwork-io%2Fbash-commons/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gruntwork-io","download_url":"https://codeload.github.com/gruntwork-io/bash-commons/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gruntwork-io%2Fbash-commons/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259101124,"owners_count":22805208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","devops","linux","scripting"],"created_at":"2024-08-01T15:06:16.099Z","updated_at":"2025-06-10T15:31:07.591Z","avatar_url":"https://github.com/gruntwork-io.png","language":"Shell","readme":"[![Maintained by Gruntwork.io](https://img.shields.io/badge/maintained%20by-gruntwork.io-%235849a6.svg)](https://gruntwork.io/?ref=repo_bash_commons)\n# Bash Commons\n\nThis repo contains a collection of reusable Bash functions for handling common tasks such as logging, assertions,\nstring manipulation, and more. It is our attempt to bring a little more sanity, predictability, and coding reuse to our\nBash scripts. All the code has thorough automated tests and is packaged into functions, so you can safely import it\ninto your bash scripts using `source`.\n\n\n\n\n## Examples\n\nOnce you have `bash-commons` installed (see the [install instructions](#install)), you use `source` to import the\nmodules and start calling the functions within them. Before you import any modules, make sure you `source` the\n`bootstrap.sh` file which sets some important defaults to encourage good code:\n\n```bash\nsource /opt/gruntwork/bash-commons/bootstrap.sh\nsource /opt/gruntwork/bash-commons/log.sh\nsource /opt/gruntwork/bash-commons/assert.sh\nsource /opt/gruntwork/bash-commons/os.sh\n\nlog_info \"Hello, World!\"\n\nassert_not_empty \"--foo\" \"$foo\" \"You must provide a value for the --foo parameter.\"\n\nif os_is_ubuntu \"16.04\"; then\n  log_info \"This script is running on Ubuntu 16.04!\"\nelif os_is_centos; then\n  log_info \"This script is running on CentOS!\"\nfi\n```\n\n## Install\n\nThe first step is to download the code onto your computer.\n\nThe easiest way to do this is with the [Gruntwork Installer](https://github.com/gruntwork-io/gruntwork-installer)\n(note, you'll need to replace `\u003cVERSION\u003e` below with a version number from the [releases\npage](https://github.com/gruntwork-io/bash-commons/releases)):\n\n```bash\ngruntwork-install \\\n  --repo https://github.com/gruntwork-io/bash-commons \\\n  --module-name bash-commons \\\n  --tag \u003cVERSION\u003e\n```\n\nThe default install location is `/opt/gruntwork/bash-commons`, but you can override that using the `dir` param, and\noverride the owner of the install dir using the `owner` and `group` params:\n\n```bash\ngruntwork-install \\\n  --repo https://github.com/gruntwork-io/bash-commons \\\n  --module-name bash-commons \\\n  --tag \u003cVERSION\u003e \\\n  --module-param dir=/foo/bar \\\n  --module-param owner=my-os-username \\\n  --module-param group=my-os-group\n```\n\nIf you don't want to use the Gruntwork Installer, you can use `git clone` to get the code onto your computer and then\ncopy it to it's final destination manually:\n\n```bash\ngit clone --branch \u003cVERSION\u003e https://github.com/gruntwork-io/bash-commons.git\n\nsudo mkdir -p /opt/gruntwork\ncp -r bash-commons/modules/bash-commons/src /opt/gruntwork/bash-commons\nsudo chown -R \"my-os-username:my-os-group\" /opt/gruntwork/bash-commons\n```\n\n## Instance Metadata Service versions\n\n`bash-commons` supports both Instance Metadata Service (IMDS) version 1 and 2. Gruntwork and AWS both recommend using version 2 of the Instance Metadata Service whenever possible. Although version 1 is still supported and considered fully secure by AWS, version 2 has been specially hardened against specific threat vectors and is therefore preferable.  Version 2 is now the default since all new instances support it by default.\n\nTo understand more about Instance Metadata Service version 2 and its features, read [the official AWS documentation on IMDSv2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html).\n\nIf you need help detecting what is still using IMDSv1 AWS has a PacketAnalyzer:\nhttps://github.com/aws/aws-imds-packet-analyzer\n\nThere are two ways to specify the version of the Instance Metadata Service that `bash-commons` should use:\n\n1. Set the environment variable `GRUNTWORK_BASH_COMMONS_IMDS_VERSION` to the version of IMDS that you wish to use. Valid values are either `1` or `2`.\n2. Change the value of `default_instance_metadata_version` to either `1` or `2`.\n\n#### Example of `dynamic-ubuntu-wait.sh` usage:\n\nYou can use the `dynamic-ubuntu-wait.sh` command after you [install bash-commons](#install):\n\n```\nbash /opt/gruntwork/bash-commons/dynamic-ubuntu-wait.sh\n```\n\nAlternatively, you can call the script without installing by curling it during your existing provisioning/automated installation process:\n\n```bash\ncurl -LsS https://raw.githubusercontent.com/gruntwork-io/bash-commons/[VERSION]/modules/bash-commons/src/dynamic-ubuntu-wait.sh | bash`\n```\n\nWhere `[VERSION]` could be: `v0.0.3`. The latest release can be found [here](https://github.com/gruntwork-io/bash-commons/releases/latest)\n\n\n\n\n## Importing modules\n\nYou can use the `source` command to \"import\" the modules you need and use them in your code:\n\n```bash\nsource /opt/gruntwork/bash-commons/log.sh\n```\n\nThis will make all the functions within that module available in your code:\n\n```bash\nlog_info \"Hello, World!\"\n```\n\n\n\n\n## Available modules\n\nHere's an overview of the modules available in `bash-commons`:\n\n* `array.sh`: Helpers for working with Bash arrays, such as checking if an array contains an element, or joining an\n  array into a string with a delimiter between elements.\n\n* `assert.sh`: Assertions that check a condition and exit if the condition is not met, such as asserting a variable is\n  not empty or that an expected app is installed. Useful for defensive programming.\n\n* `aws.sh`: A collection of thin wrappers for direct calls to the [AWS CLI](https://aws.amazon.com/cli/) and [EC2\n  Instance Metadata](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html). These thin\n  wrappers give you a shorthand way to fetch certain information (e.g., information about an EC2 Instance, such as its\n  private IP, public IP, Instance ID, and region). Moreover, you can swap out `aws.sh` with a version that returns mock\n  data to make it easy to run your code locally (e.g., in Docker) and to run unit tests. This requires IMDS to be enabled.\n\n* `aws-wrapper.sh`: A collection of \"high level\" wrappers for the [AWS CLI](https://aws.amazon.com/cli/) and [EC2\n  Instance Metadata](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) to simplify common\n  tasks such as looking up tags or IPs for EC2 Instances. Note that these wrappers handle all the data processing and\n  logic, whereas all the direct calls to the AWS CLI and EC2 metadata endpoints are delegated to `aws.sh` to make unit\n  testing easier.\n\n* `dynamic-ubuntu-wait.sh`: A script that dynamically waits for Ubuntu automatic update mechanism to\n release all locks so that `apt-get` may run without errors.\n\n* `file.sh`: A collection of helpers for working with files, such as checking if a file exists or contains certain text.\n\n* `log.sh`: A collection of logging helpers that write logs to `stderr` with log levels (INFO, WARN, ERROR) and\n  timestamps.\n\n* `os.sh`: A collection of Operating System helpers, such as checking which flavor of Linux (e.g., Ubuntu, CentOS) is\n  running and validating checksums.\n\n* `string.sh`: A collection of string manipulation functions, such as checking if a string contains specific text,\n  stripping prefixes, and stripping suffixes.\n  \n\n\n## Coding principles\n\nThe code in `bash-commons` follows the following principles:\n\n1. [Compatibility](#compatibility)\n1. [Code style](#code-style)\n1. [Everything is a function](#everything-is-a-function)\n1. [Namespacing](#namespacing)\n1. [Testing](#testing)\n\n\n### Compatibility\n\nThe code in this repo aims to be compatible with:\n\n* Bash 3\n* Most major Linux distributions (e.g., Ubuntu, CentOS)\n\n\n### Code style\n\nAll the code should mainly follow the [Google Shell Style Guide](https://google.github.io/styleguide/shell.xml).\nIn particular:\n\n* The first line of every script should be `#!/usr/bin/env bash`.\n* All code should be defined in functions.\n* Functions should exit or return 0 on success and non-zero on error.\n* Functions should return output by writing it to `stdout`.\n* Functions should log to `stderr`.\n* All variables should be `local`. No global variables are allowed at all.\n* Make as many variables `readonly` as possible.\n* If a variable is both local and readonly, use `local -r`. \n* If calling to a subshell and storing the output in a variable (foo=`$( ... )`), do NOT use `local -r`  in the same\n  statement or the [exit code will be lost](https://blog.gruntwork.io/yak-shaving-series-1-all-i-need-is-a-little-bit-of-disk-space-6e5ef1644f67).\n  Instead, declare the variable as `local` on one line and then call the subshell on the next line.\n* Quote all strings.\n* Use `[[ ... ]]` instead of `[ ... ]`.\n* Use snake_case for function and variable names. Use UPPER_SNAKE_CASE for constants.\n\n\n### Everything in a function\n\nIt's essential that ALL code is defined in a function. That allows you to use `source` to \"import\" that code without\nanything actually being executed.\n\n\n### Namespacing\n\nBash does not support namespacing, so we fake it using a convention on the function names: if you create a file\n`\u003cfoo.sh\u003e`, all functions in it should start with `foo_`. For example, all the functions in `log.sh` start with `log_`\n(`log_info`, `log_error`) and all the functions in `string.sh` start with `string_` (`string_contains`,\n`string_strip_prefix`). That makes it easier to tell which functions came from which modules.\n\nFor readability, that means you should typically give files a name that is a singular noun. For example, `log.sh`\ninstead of `logging.sh` and `string.sh` instead of `strings.sh`.\n\n\n### Testing\n\nEvery function should be tested:\n\n* Automated tests are in the [test](/test) folder.\n\n* We use [Bats](https://github.com/sstephenson/bats) as our unit test framework for Bash code. Note: Bats has not been\n  maintained the last couple years, so we may need to change to the [bats-core](https://github.com/bats-core/bats-core)\n  fork at some point (see [#150](https://github.com/sstephenson/bats/issues/150)).\n\n* We run all tests in the [gruntwork/bash-commons-circleci-tests Docker\n  image](https://hub.docker.com/r/gruntwork/bash-commons-circleci-tests/) so that (a) it's consistent with how the CI\n  server runs them, (b) the tests always run on Linux, (c) any changes the tests make, such as writing files or\n  creating OS users, won't affect the host OS, (d) we can replace some of the modules, such as `aws.sh`, with mocks at\n  test time. There is a `docker-compose.yml` file in the `test` folder to make it easy to run the tests.\n\n* To run all the tests: `docker-compose up`.\n\n* To run one test file: `docker-compose run tests bats test/array.bats`.\n\n* To leave the Docker container running so you can debug, explore, and interactively run bats: `docker-compose run tests bash`.\n\n* If you ever need to build a new Docker image, the `Dockerfile` is in the [.circleci folder](/.circleci):\n\n    ```bash\n    cd .circleci\n    docker build -t gruntwork/bash-commons-circleci-tests .\n    docker push gruntwork/bash-commons-circleci-tests\n    ```\n\n\n\n## TODO\n\n1. Add automated tests for `aws.sh` and `aws-wrapper.sh`. We have not tested these as they require either running an\n   EC2 Instance or run something like [LocalStack](https://github.com/localstack/localstack).\n\n## License\n\nThis code is released under the Apache 2.0 License. Please see \n[LICENSE](https://github.com/gruntwork-io/bash-commons/tree/master/LICENSE) and \n[NOTICE](https://github.com/gruntwork-io/bash-commons/tree/master/NOTICE) for more details.\n\nCopyright \u0026copy; 2018 Gruntwork, Inc.\n","funding_links":["https://github.com/sponsors/gruntwork-io"],"categories":["Shell","bash"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgruntwork-io%2Fbash-commons","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgruntwork-io%2Fbash-commons","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgruntwork-io%2Fbash-commons/lists"}