58

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

chicks
  • 3,639
  • 10
  • 26
  • 36
skalee
  • 693
  • 1
  • 5
  • 6

5 Answers5

83

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

Wayne
  • 931
  • 1
  • 6
  • 3
  • 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 03 '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` – lmsurprenant Nov 01 '17 at 22:50
  • 1
    never mind, i should have just looked it up: https://stackoverflow.com/questions/10938483/bash-specifying-environment-variables-for-echo-on-command-line – lmsurprenant Nov 01 '17 at 22:57
30
begin; set -lx EDITOR vim; crontab -e; end
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
13

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

begin
  set -lx SOME_VAR 1
  command
end
Azd325
  • 231
  • 2
  • 4
2

depending on a definition of be function, this can fail

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

In order for it to work:

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

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.

1

Starting version 3.1, you can use the same syntax used in bash (EDITOR=vim crontab -e).

PR introducing the feature: https://github.com/fish-shell/fish-shell/pull/6287