How to make it such that, when running `command`, it automatically does `SOME_ENV_VAR=value command`? (something cleaner than aliases?)

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?

Kajika,

You can add a new executable in your ~/.local/bin directory like command_custom that would start SOME_ENV_VAR=value command. Like if you use bash:

<pre style="background-color:#ffffff;">
<span style="font-style:italic;color:#969896;">#!/usr/bin/bash
</span><span style="color:#323232;">
</span><span style="color:#323232;">SOME_ENV_VAR</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#183691;">value </span><span style="color:#62a35c;">command
</span>

Do not forget to chmod +x the file to make it executable.

This way you will have additional command for your user only (no sudo require to create/update those), for system-wise command put it in /usr/local/bin.

manned_meatball,
@manned_meatball@lemmy.ml avatar
<pre style="background-color:#ffffff;">
<span style="color:#323232;">function command_one() {
</span><span style="color:#323232;">    # activate the environment
</span><span style="color:#323232;">    source "$XDG_DATA_HOME/venvs/alpha.sh"
</span><span style="color:#323232;">    # run the thing
</span><span style="color:#323232;">    actual_command_one
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">function command_two() {
</span><span style="color:#323232;">    # activate the environment
</span><span style="color:#323232;">    source "$XDG_DATA_HOME/venvs/alpha.sh"
</span><span style="color:#323232;">    source "$XDG_DATA_HOME/venvs/bravo.sh"
</span><span style="color:#323232;">    # run the other thing
</span><span style="color:#323232;">    actual_command_two
</span><span style="color:#323232;">}
</span>
heartlessevil,

Is this a use case where you can use dotenv? (folder specific environment variables?)

If it’s not, aliases are the best you can do, or bash functions that are equivalent to them. The thing is that those only run in bash, so if you are expecting to run the commands outside of a shell, you will need to wrap them in bash -c or have a wrapper script.

This is just the broad strokes so if you have any questions please follow up.

nicoag328,

You could source an aliases.sh file on your .bashrc where you define your aliases, so that they don’t fill up your bashrc.

For example, in your bashrc:

source ~/.aliases.sh

This way you could also create a file with aliases per program.

cyclohexane,

That’s a good idea, but it only makes the problem a little better. I still wouldn’t want one large aliases.sh file with environment variables for every application I customized. Would rather have them separate somehow without gobbling up a file

solidgrue,
@solidgrue@lemmy.world avatar

You could alias the your programs’ commands to invoke the environment variables.

Or, use an alias to source an environment file before launching the binary?

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • [email protected]
  • aaaaaaacccccccce
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • oklahoma
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • Ask_kbincafe
  • SuperSentai
  • feritale
  • KamenRider
  • All magazines