In bash, I can do EDITOR=vim crontab -e. Can I get similar effect in Fish shell?

begin; set -lx EDITOR vim; crontab -e; end
  • 3
    is there any easier way to do this? Oct 29 '12 at 8:43
  • oddly, this doesn't work for me. I'm calling a ruby script, and ENV doesn't pick up the variable I'm setting: set -lx date '12/04/2012'
    – Duke
    Dec 7 '12 at 23:13
  • @Duke: It works for me. begin; set -lx date '12/04/2012'; ruby -e 'puts ENV["date"]'; end Dec 8 '12 at 0:49
  • 10
    In the meanwhile this has been answered in the Fish FAQ: fishshell.com/docs/current/faq.html#faq-single-env
    – harm
    Apr 3 '14 at 9:09
  • 2
    And what that FAQ says is this: env SOME_VAR=1 command Jun 5 '18 at 20:54

Don't see why this shouldn't work: env EDITOR=vim crontab -e
That bypasses the shell completely.

  • 4
    This is so much easier. The only problem is that bypassing the shell disallows any custom fish commands, which are probably locked in your muscle memory.
    – JohnMetta
    Jun 3 '13 at 20:11
  • 1
    I saw that in the docs too, but then why doesn't the following work? env SOME_VAR=1 echo $SOME_VAR Nov 1 '17 at 22:50
  • 1
    never mind, i should have just looked it up: stackoverflow.com/questions/10938483/… Nov 1 '17 at 22:57

That is from the Documentation

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 and that would not bypass the shell

  set -lx SOME_VAR 1

depending on a definition of be function, this can fail

  set -lx RAILS_ENV staging
  be rails r "p ENV['RAILS_ENV']"

In order for it to work:

function be --description 'Runs bundle exec' --no-scope-shadowing
  bundle exec $argv

Please, see the explanation of --no-scope-shadowing option

-S or --no-scope-shadowing allows the function to access the variables of calling functions. Normally, any variables inside the function that have the same name as variables from the calling function are "shadowed", and their contents is independent of the calling function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.