hello friends,

I am looking for a way to do what I described in the title. When running command command, I dont want to have to type SOME_ENV_VAR=value command every time, especially if there are multiple.

I am sure youre immediately thinking aliases. My issue with aliases is that if I do this for several programs, my .bashrc will get large and messy quickly. I would prefer a way to separate those by program or application, rather than put them all in one file.

Is there a clean way to do this?

  • Andy@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    1 year ago

    If you were using Zsh, one way you could do this is by autoloading function files from a folder in your fpath.

    Let’s say you’re using ~/.local/share/zsh/site-functions for your custom functions. To ensure that folder is an early part of your fpath, put something like this within your .zshrc:

    typeset -U fpath=(~/.local/share/zsh/site-functions $fpath)
    

    Then let’s say you want to override the uptime command. Add a file ~/.local/share/zsh/site-functions/uptime with content like:

    NO_COLOR=1 =uptime
    
    Explanation for the second =:
     `=' expansion
         If  a word begins with an unquoted `=' and the EQUALS option is set, the remainder of the word is taken as the name of a command.  If a command ex‐
         ists by that name, the word is replaced by the full pathname of the command.
    

    The last thing you need to do is mark it for autoloading, in your .zshrc:

    autoload -Uz uptime
    

    Instead of listing those functions manually as arguments, you could instead use a glob pattern to collect all those names, excluding any which begin with _ (completion functions):

    autoload -Uz ~/.local/share/zsh/site-functions/[^_]*(:t)