aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc_src/set.txt
blob: 0b3d8417367fc19f86d30d780d2ec8cabdbab858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
\section set set - display and change shell variables.

\subsection set-synopsis Synopsis
\fish{synopsis}
set [SCOPE_OPTIONS]
set [OPTIONS] VARIABLE_NAME VALUES...
set [OPTIONS] VARIABLE_NAME[INDICES]... VALUES...
set ( -q | --query ) [SCOPE_OPTIONS] VARIABLE_NAMES...
set ( -e | --erase ) [SCOPE_OPTIONS] VARIABLE_NAME
set ( -e | --erase ) [SCOPE_OPTIONS] VARIABLE_NAME[INDICES]...
\endfish

\subsection set-description Description

`set` manipulates <a href="index.html#variables">shell variables</a>.

If set is called with no arguments, the names and values of all shell variables are printed. If some of the scope or export flags have been given, only the variables matching the specified scope are printed.

With both variable names and values provided, `set` assigns the variable `VARIABLE_NAME` the values `VALUES...`.

The following options control variable scope:

- `-l` or `--local` forces the specified shell variable to be given a scope that is local to the current block, even if a variable with the given name exists and is non-local

- `-g` or `--global` causes the specified shell variable to be given a global scope. Non-global variables disappear when the block they belong to ends

- `-U` or `--universal` causes the specified shell variable to be given a universal scope. If this option is supplied, the variable will be shared between all the current users fish instances on the current computer, and will be preserved across restarts of the shell.

- `-x` or `--export` causes the specified shell variable to be exported to child processes (making it an "environment variable")

- `-u` or `--unexport` causes the specified shell variable to NOT be exported to child processes


The following options are available:

- `-e` or `--erase` causes the specified shell variable to be erased

- `-q` or `--query` test if the specified variable names are defined. Does not output anything, but the builtins exit status is the number of variables specified that were not defined.

- `-n` or `--names` List only the names of all defined variables, not their value

- `-L` or `--long` do not abbreviate long values when printing set variables


If a variable is set to more than one value, the variable will be an array with the specified elements. If a variable is set to zero elements, it will become an array with zero elements.

If the variable name is one or more array elements, such as `PATH[1 3 7]`, only those array elements specified will be changed. When array indices are specified to `set`, multiple arguments may be used to specify additional indexes, e.g. `set PATH[1] PATH[4] /bin /sbin`. If you specify a negative index when expanding or assigning to an array variable, the index will be calculated from the end of the array. For example, the index -1 means the last index of an array.

The scoping rules when creating or updating a variable are:

-# If a variable is explicitly set to either universal, global or local, that setting will be honored. If a variable of the same name exists in a different scope, that variable will not be changed.

-# If a variable is not explicitly set to be either universal, global or local, but has been previously defined, the previous variable scope is used.

-# If a variable is not explicitly set to be either universal, global or local and has never before been defined, the variable will be local to the currently executing function. Note that this is different from using the `-l` or `--local` flag. If one of those flags is used, the variable will be local to the most inner currently executing block, while without these the variable will be local to the function. If no function is executing, the variable will be global.


The exporting rules when creating or updating a variable are identical to the scoping rules for variables:

-# If a variable is explicitly set to either be exported or not exported, that setting will be honored.

-# If a variable is not explicitly set to be exported or not exported, but has been previously defined, the previous exporting rule for the variable is kept.

-# If a variable is not explicitly set to be either exported or unexported and has never before been defined, the variable will not be exported.


In query mode, the scope to be examined can be specified.

In erase mode, if variable indices are specified, only the specified slices of the array variable will be erased.

`set` requires all options to come before any other arguments. For example, `set flags -l` will have the effect of setting the value of the variable `flags` to '-l', not making the variable local.

In assignment mode, `set` exits with a non-zero exit status if variable assignments could not be successfully performed. If the variable assignments were performed, the exit status is unchanged. This allows simultaneous capture of the output and exit status of a subcommand, e.g. `if set output (command)`. In query mode, the exit status is the number of variables that were not found. In erase mode, `set` exits with a zero exit status in case of success, with a non-zero exit status if the commandline was invalid, if the variable was write-protected or if the variable did not exist.


\subsection set-example Example
\fish
set -xg
# Prints all global, exported variables.

set foo hi
# Sets the value of the variable $foo to be 'hi'.

set -e smurf
# Removes the variable $smurf

set PATH[4] ~/bin
# Changes the fourth element of the $PATH array to ~/bin

if set python_path (which python)
    echo "Python is at $python_path"
end
# Outputs the path to Python if `which` returns true.
\endfish