New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setenv vs set -x vs export #2704

Closed
franciscolourenco opened this Issue Feb 2, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@franciscolourenco

franciscolourenco commented Feb 2, 2016

Is there a difference between these, and is there a preferred way to set env variables?

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Feb 2, 2016

The fish way to do this is using the set builtin.

set -x foo

In the example above, foo is an "environment" variable local to the current scope. If you run that code inside a function it will be local to the function. Instead, if you run that code at the CLI it will be global to the current fish session.

Either way, the variable will be available to all children processes created from the same scope.

function foo
    set -lx IFS "@"
    bar
end

In the example above, we export IFS as "@". This way it will be visible inside bar (let's assume bar is an external utility like one found in /local/usr/bin), but as soon as we exit the function foo, IFS will not have changed.

As for setenv I guess you use it to accomplish similar feats in other shells, but in fish you are better off using set -x which is guaranteed to work acrosss other systems also running fish.

Here is the documentation for set.

ghost commented Feb 2, 2016

The fish way to do this is using the set builtin.

set -x foo

In the example above, foo is an "environment" variable local to the current scope. If you run that code inside a function it will be local to the function. Instead, if you run that code at the CLI it will be global to the current fish session.

Either way, the variable will be available to all children processes created from the same scope.

function foo
    set -lx IFS "@"
    bar
end

In the example above, we export IFS as "@". This way it will be visible inside bar (let's assume bar is an external utility like one found in /local/usr/bin), but as soon as we exit the function foo, IFS will not have changed.

As for setenv I guess you use it to accomplish similar feats in other shells, but in fish you are better off using set -x which is guaranteed to work acrosss other systems also running fish.

Here is the documentation for set.

@faho

This comment has been minimized.

Show comment
Hide comment
@faho

faho Feb 2, 2016

Member

There's an additional point:

> type set
set is a builtin
> type setenv
setenv is a function with definition 
[...]
> type export
export is a function with definition
[...]

We ship functions called export and setenv for compatibility. These are simple shims that can be used to help you ease in to fish. If you wish to write idiomatic fish code, use set -x. (There's also a bit of overhead associated with these functions, but it should be minimal)

Member

faho commented Feb 2, 2016

There's an additional point:

> type set
set is a builtin
> type setenv
setenv is a function with definition 
[...]
> type export
export is a function with definition
[...]

We ship functions called export and setenv for compatibility. These are simple shims that can be used to help you ease in to fish. If you wish to write idiomatic fish code, use set -x. (There's also a bit of overhead associated with these functions, but it should be minimal)

@franciscolourenco

This comment has been minimized.

Show comment
Hide comment
@franciscolourenco

franciscolourenco Feb 3, 2016

Thank you for the detailed explanation!

franciscolourenco commented Feb 3, 2016

Thank you for the detailed explanation!

@faho faho added the question label Mar 6, 2016

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