Skip to content

A function for importing dependencies into bash scripts.

License

Notifications You must be signed in to change notification settings

gistbucket/bash-import-source

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bash-imports

This is a small bash script I wrote to help with reusing shell code. It provides a shell function named import for importing "modules", which is basically a more general version of source. In fact, import ultimatly just calls source, but it searches for the file (or files) to source. For example:

#! /bin/bash
source import_utils
import functions

...

In this script, the import function will look for a file or directory named:

  1. functions
  2. functions.sh
  3. functions.tar
  4. functions.tar.bz2
  5. functions.tar.gz

If found, the import function will do the following:

  1. If a file is found, it is sourced.
  2. If a directory is found:
    1. Source all *.sh files in the top-level of the directory.
    2. Source all *.sh files in the lib/ subdirectory, if it exists.
    3. Add the bin/ subdirectory to PATH.
  3. If a tarball is found:
    1. Unpack the tarball into a directory.
    2. Source all *.sh files in the top-level of the directory.
    3. Source all *.sh files in the lib/ subdirectory, if it exists.
    4. Add the bin/ subdirectory to PATH.

The module function will search for modules in the current directory first. If the module is not found, it will start searching in the list of directories contained in the IMPORT_MODULE_PATH (set to ${HOME}/.bash/modules by default) environment variable. The first file or directory found is used.

This makes it very easy to reuse utility code in your shell scripts. Just wrap the code up into shell functions, save them to a file, and drop it in ${HOME}/.bash/modules. Then your script can source the import_utils script, and import the file containing your utility code.

About

A function for importing dependencies into bash scripts.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 84.7%
  • Perl 15.3%