Skip to content

Instantly share code, notes, and snippets.

@cloud8421
Created October 6, 2012 08:55
Show Gist options
  • Save cloud8421/3844435 to your computer and use it in GitHub Desktop.
Save cloud8421/3844435 to your computer and use it in GitHub Desktop.
Running commands with local variables on the fish shell

When using the Fish shell, you may need to run commands with local variables, like the following (Bash):

RAILS_ENV=staging rake db:migrate

This doesn't work in Fish, as setting a local variable follows another syntax, using the set command.

You can achieve the same result with the following syntax:

set -lx RAILS_ENV staging; rake db:migrate

The flags set the variable as local (l) and export it to child processes (x). You may not need the latter depending on the command you want to run.

It's a bit verbose, so you can define a function to make it easier

function v
  set -lx $args
end

Then you can type:

v RAILS_ENV staging; rake db:migrate
@dukejones
Copy link

This is not the same, though, as the environment variable remains set in the particular shell. This could cause issues if you later expect RAILS_ENV to be development in the same shell session.

@dukejones
Copy link

looks like you'll have to declare a block to get the variable to fall out of scope:
begin; set -lx RAILS_ENV staging; rake db:migrate; end

which, IMO, is way too verbose and is the biggest thing missing in fish.

@mixmastamyk
Copy link

If it is an external command, try the env command:

env PAGER=less foo --help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment