user10649607
user10649607

Reputation: 11

How to set variables in Fish shell?

The set command in my fish shell in Ubuntu (elementary OS and Linux Mint) doesn't work. The variables stay empty and even the examples in the tutorials don't work, i.e.:

(set foo hi --> # Sets the value of the variable $foo to be 'hi'.).

In the script I am trying to do the following,

set COUNTRY US CN MX

but when I want to call the variable by $COUNTRY there is no answer.

What am I doing wrong?

Upvotes: 0

Views: 3698

Answers (1)

Kurtis Rader
Kurtis Rader

Reputation: 7459

Are you really using the fish shell? Because your example works for me:

$ set COUNTRY US CN MX
$ echo $COUNTRY
US CN MX
$ set --show COUNTRY
$COUNTRY: not set in local scope
$COUNTRY: set in global scope, unexported, with 3 elements
$COUNTRY[1]: length=2 value=|US|
$COUNTRY[2]: length=2 value=|CN|
$COUNTRY[3]: length=2 value=|MX|
$COUNTRY: not set in universal scope

I suspect you are trying to use COUNTRY in another process. In which case you need to export the var using set -x COUNTRY US CN MX. But note that fish vars are arrays and exporting a var with more than one value won't be intelligible to a child process (unless that child process is a fish shell).

Also, it sounds like you might be trying to modify a variable in a parent process via a fish script. That won't work. You cannot modify the variables of a parent process. Not even if they are an environment variable. This is not a fish limitation. It is inherent in the design of the UNIX process model.

Upvotes: 2

Related Questions