DEV Community

nabbisen
nabbisen

Posted on • Edited on

Shells: How To Use A Modified Environment In Commands

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 🐈

Top comments (4)

Collapse
 
dbanty profile image
Dylan Anthony

What are the benefits of fish? Or I guess any alternative shell. I’ve always used Bash mostly because it comes with macOS (on my Ubuntu servers), but I constantly see references to zsh and fish.

Collapse
 
nabbisen profile image
nabbisen • Edited

Hi, Dylan, thank you for your sending a question :)
You use macOS and Ubuntu.

The benefits of fish I am especially given are:

  • Out of the box.
  • Auto suggestions referring to both of user's path and history. Very great.
  • Lightweight with so high functionality.

On the other hand, one of fish's cons is perhaps not to support POSIX, so some may feel bewildered because of the difference of the grammars.
I am one who don't care about it.

I think it's about not "which is better" but "which is pleasant for me in the situation" :)
Besides, I sometimes use bash with Linux or tcsh with FreeBSD.

Well, have you ever read this Boris' great article?

I believe It helps you :)

Collapse
 
dbanty profile image
Dylan Anthony

Thank! I'll check it out.

Collapse
 
nabbisen profile image
nabbisen • Edited

Hi, Erick, thank you so much for your kind lesson.
I have updated my post following your messages :)
I have to learn more and also use right words.