Search code examples
bashconstantscode-cleanup

Bash - How to share constants between scripts?


Is there a way to make it so that there isn't a list of constants at the top of every bash script i have? I have a lot of readonly constants for formatting and such but having them at the top of every single one of my scripts takes up space. Is there a way to make a separate script containing all of them and access it or something? or some way to clean it up?


Solution

  • Use a common script that you source in your other scripts.

    E.g. source common.sh will execute that file and you may export variables there.

    This is also how files like .bashrc and .bash_profile work, it is sourced when you execute bash (the latter when it is a login shell). When you change .bashrc, you can source it again in the running shell for the changes to take effect.