Shells: How To Use A Modified Environment In Commands

nabbisen - Apr 26 '19 - - Dev Community

Introduction

I love fish 🐟 shell because of its convenient auto suggestions and excellent performance.

I had, however, a slightly serious problem on switching from bash.
I didn't know how to use a modified environment in commands.

For example, I was in search for how to change the names of home subdirectories, and found the bash solution like this 🤔:

LANG=C xdg-user-dirs-gtk-update

This means running the xdg-user-dirs-gtk-update command with setting "C" for the LANG environment.
Running the same command failed in fish shell...🥴

I, however, didn't feel like always using an additional step such as the bash command or set.

I wrote this post so as to demonstrate how to solve it.

* Besides: Just today, I found fish prints the great error message. Holy mackerel! :

fish: Unsupported use of '='. To run 'xdg-user-dirs-gtk-update' with a modified environment, please use 'env LANG=C xdg-user-dirs-gtk-update…'

Body

In order to set a modified environment, it's possible in bash by using the SOME_ENV=value format at the top:

EDITOR=vi git commit

It's also the same about ksh / zsh.

But not about fish!

There isn't any solution? Yes. It's possible if we use env command 😊:

SOME_VAR=1 command produces an error: Unknown command "SOME_VAR=1".
Use the env command.
env SOME_VAR=1 command
You can also declare a local variable in a block:

The env SOME_ENV=value format works nicely!!

env SOME_ENV=vi git commit

It's the same about tcsh.

Conclusion

Shell Version Grammar
bash 5.0.2

SOME_ENV=value command

*Note: It seems ignored if env is added to the top of commands.

ksh 5.2.14
zsh 5.7.1
fish 3.0.2 env SOME_ENV=value command
tsch 6.20.0

References And My Thanks To

Happy operating 🐈

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .