2019-03-31 17:05:09 +08:00
.. _cmd-set:
2020-03-31 23:37:38 +08:00
set - display and change shell variables
========================================
2018-12-17 09:39:33 +08:00
2018-12-18 09:58:24 +08:00
Synopsis
--------
2018-12-17 05:08:41 +08:00
docs synopsis: add HTML highlighing and automate manpage markup
Recent synopsis changes move from literal code blocks to
[RST line blocks]. This does not translate well to HTML: it's not
rendered in monospace, so aligment is lost. Additionally, we don't
get syntax highlighting in HTML, which adds differences to our code
samples which are highlighted.
We hard-wrap synopsis lines (like code blocks). To align continuation
lines in manpages we need [backslashes in weird places]. Combined with
the **, *, and `` markup, it's a bit hard to get the alignment right.
Fix these by moving synopsis sources back to code blocks and compute
HTML syntax highlighting and manpage markup with a custom Sphinx
extension.
The new Pygments lexer can tokenize a synopsis and assign the various
highlighting roles, which closely matches fish's syntax highlighing:
- command/keyword (dark blue)
- parameter (light blue)
- operator like and/or/not/&&/|| (cyan)
- grammar metacharacter (black)
For manpage output, we don't project the fish syntax highlighting
but follow the markup convention in GNU's man(1):
bold text type exactly as shown.
italic text replace with appropriate argument.
To make it easy to separate these two automatically, formalize that
(italic) placeholders must be uppercase; while all lowercase text is
interpreted literally (so rendered bold).
This makes manpages more consistent, see string-join(1) and and(1).
Implementation notes:
Since we want manpage formatting but Sphinx's Pygments highlighing
plugin does not support manpage output, add our custom "synopsis"
directive. This directive parses differently when manpage output is
specified. This means that the HTML and manpage build processes must
not share a cache, because the parsed doctrees are cached. Work around
this by using separate cache locations for build targets "sphinx-docs"
(which creates HTML) and "sphinx-manpages". A better solution would
be to only override Sphinx's ManualPageBuilder but that would take a
bit more code (ideally we could override ManualPageWriter but Sphinx
4.3.2 doesn't really support that).
---
Alternative solution: stick with line blocks but use roles like
:command: or :option: (or custom ones). While this would make it
possible to produce HTML that is consistent with code blocks (by adding
a bit of CSS), the source would look uglier and is harder to maintain.
(Let's say we want to add custom formatting to the [|] metacharacters
in HTML. This is much easier with the proposed patch.)
---
[RST line blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#line-blocks
[backslashes in weird places]: https://github.com/fish-shell/fish-shell/pull/8626#discussion_r782837750
2022-01-09 22:09:46 +08:00
.. synopsis ::
2022-01-17 12:29:49 +08:00
set [SCOPE_OPTIONS]
set [OPTIONS] VARIABLE VALUES ...
set [OPTIONS] VARIABLE[INDICES] VALUES ...
set (-q | --query) [SCOPE_OPTIONS] VARIABLE ...
set (-e | --erase) [SCOPE_OPTIONS] VARIABLE ...
set (-e | --erase) [SCOPE_OPTIONS] VARIABLE[INDICES] ...
docs synopsis: add HTML highlighing and automate manpage markup
Recent synopsis changes move from literal code blocks to
[RST line blocks]. This does not translate well to HTML: it's not
rendered in monospace, so aligment is lost. Additionally, we don't
get syntax highlighting in HTML, which adds differences to our code
samples which are highlighted.
We hard-wrap synopsis lines (like code blocks). To align continuation
lines in manpages we need [backslashes in weird places]. Combined with
the **, *, and `` markup, it's a bit hard to get the alignment right.
Fix these by moving synopsis sources back to code blocks and compute
HTML syntax highlighting and manpage markup with a custom Sphinx
extension.
The new Pygments lexer can tokenize a synopsis and assign the various
highlighting roles, which closely matches fish's syntax highlighing:
- command/keyword (dark blue)
- parameter (light blue)
- operator like and/or/not/&&/|| (cyan)
- grammar metacharacter (black)
For manpage output, we don't project the fish syntax highlighting
but follow the markup convention in GNU's man(1):
bold text type exactly as shown.
italic text replace with appropriate argument.
To make it easy to separate these two automatically, formalize that
(italic) placeholders must be uppercase; while all lowercase text is
interpreted literally (so rendered bold).
This makes manpages more consistent, see string-join(1) and and(1).
Implementation notes:
Since we want manpage formatting but Sphinx's Pygments highlighing
plugin does not support manpage output, add our custom "synopsis"
directive. This directive parses differently when manpage output is
specified. This means that the HTML and manpage build processes must
not share a cache, because the parsed doctrees are cached. Work around
this by using separate cache locations for build targets "sphinx-docs"
(which creates HTML) and "sphinx-manpages". A better solution would
be to only override Sphinx's ManualPageBuilder but that would take a
bit more code (ideally we could override ManualPageWriter but Sphinx
4.3.2 doesn't really support that).
---
Alternative solution: stick with line blocks but use roles like
:command: or :option: (or custom ones). While this would make it
possible to produce HTML that is consistent with code blocks (by adding
a bit of CSS), the source would look uglier and is harder to maintain.
(Let's say we want to add custom formatting to the [|] metacharacters
in HTML. This is much easier with the proposed patch.)
---
[RST line blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#line-blocks
[backslashes in weird places]: https://github.com/fish-shell/fish-shell/pull/8626#discussion_r782837750
2022-01-09 22:09:46 +08:00
set (-S | --show) VARIABLE ...
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Description
2019-01-03 12:10:47 +08:00
-----------
2018-12-17 05:08:41 +08:00
2019-03-31 17:32:40 +08:00
`` set `` manipulates :ref: `shell variables <variables>` .
2018-12-17 05:08:41 +08:00
2021-12-22 09:24:47 +08:00
If both a *VARIABLE* and *VALUES* are provided, `` set `` assigns the values to the variable of that name. Because all variables in fish are :ref: `lists <variables-lists>` , multiple values are allowed.
2018-12-17 05:08:41 +08:00
2021-05-13 00:52:24 +08:00
If only a variable name has been given, `` set `` sets the variable to the empty list.
2018-12-17 05:08:41 +08:00
2021-05-13 00:52:24 +08:00
If `` set `` is called with no arguments, it prints the names and values of all shell variables in sorted order. Passing :ref: `scope <variables-scope>` or :ref: `export <variables-export>` flags allows filtering this to only matching variables, so `` set --local `` would only show local variables.
2018-12-17 05:08:41 +08:00
2021-05-13 00:52:24 +08:00
With `` --erase `` and optionally a scope flag `` set `` will erase the matching variable (or the variable of that name in the smallest possible scope).
2018-12-17 05:08:41 +08:00
2021-05-13 00:52:24 +08:00
With `` --show `` , `` set `` will describe the given variable names, explaining how they have been defined - in which scope with which values and options.
The following options control variable scope:
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-f** or **--function**
Scopes the variable to the currently executing function. It is erased when the function ends.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-l** or **--local**
Scopes the variable to the currently executing block. It is erased when the block ends. Outside of a block, this is the same as **--function** .
Add `set --function` (#8145)
* Add `set --function`
This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.
This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.
E.g.:
```fish
set -l thing
if condition
set thing one
else
set thing two
end
```
could be written as
```fish
if condition
set -f thing one
else
set -f thing two
end
```
Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.
Also, if there isn't an else-branch in that above, just with
```fish
if condition
set -f thing one
end
```
that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!
Some more interesting parts:
Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:
```fish
function foo2
set -l foo bar
set -f foo baz # modifies the *same* variable!
end
```
but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:
```fish
function foo3
set -f foo bar
begin
set -l foo banana
# $foo is banana
end
# $foo is bar again
end
```
This is how local variables already work. "Local" is actually "block-scoped".
Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.
As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).
Fixes #565
2021-08-02 02:08:12 +08:00
2022-03-11 23:56:20 +08:00
**-g** or **--global**
Causes the specified shell variable to be given a global scope. Global variables don't disappear and are available to all functions running in the same shell. They can even be modified.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-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 user's fish instances on the current computer, and will be preserved across restarts of the shell.
2018-12-17 05:08:41 +08:00
2021-05-13 00:52:24 +08:00
These options control additional variable options:
2022-03-11 23:56:20 +08:00
**-x** or **--export**
Causes the specified shell variable to be exported to child processes (making it an "environment variable")
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-u** or **--unexport**
Causes the specified shell variable to NOT be exported to child processes
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**--path**
Causes the specified variable to be treated as a :ref: `path variable <variables-path>` , meaning it will automatically be split on colons, and joined using colons when quoted (`` echo "$PATH" `` ) or exported.
2019-04-30 19:18:56 +08:00
2022-03-11 23:56:20 +08:00
**--unpath**
Causes the specified variable to not be treated as a :ref: `path variable <variables-path>` . Variables with a name ending in "PATH" are automatically path variables, so this can be used to treat such a variable normally.
2018-12-17 05:08:41 +08:00
2021-05-13 00:52:24 +08:00
The following other options are available:
2022-03-11 23:56:20 +08:00
**-a** or **--append**
Causes the values to be appended to the current set of values for the variable. This can be used with **--prepend** to both append and prepend at the same time. This cannot be used when assigning to a variable slice.
2021-05-13 00:52:24 +08:00
2022-03-11 23:56:20 +08:00
**-p** or **--prepend**
Causes the values to be prepended to the current set of values for the variable. This can be used with **--append** to both append and prepend at the same time. This cannot be used when assigning to a variable slice.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-e** or **--erase**
Causes the specified shell variables to be erased
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-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, up to a maximum of 255. If no variable was given, it also returns 255.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-n** or **--names**
List only the names of all defined variables, not their value. The names are guaranteed to be sorted.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-S** or **--show**
Shows information about the given variables. If no variable names are given then all variables are shown in sorted order. It shows the scopes the given variables are set in, along with the values in each and whether or not it is exported. No other flags can be used with this option.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-L** or **--long**
Do not abbreviate long values when printing set variables.
2018-12-17 05:08:41 +08:00
2022-03-12 00:18:41 +08:00
**-h** or **--help**
Displays help about using this command.
2019-05-05 20:01:07 +08:00
If a variable is set to more than one value, the variable will be a list with the specified elements. If a variable is set to zero elements, it will become a list with zero elements.
2018-12-17 05:08:41 +08:00
2019-05-05 20:01:07 +08:00
If the variable name is one or more list elements, such as `` PATH[1 3 7] `` , only those list elements specified will be changed. If you specify a negative index when expanding or assigning to a list variable, the index will be calculated from the end of the list. For example, the index -1 means the last index of a list.
2018-12-17 05:08:41 +08:00
The scoping rules when creating or updating a variable are:
2019-06-05 04:07:06 +08:00
- Variables may be explicitly set to universal, global or local. Variables with the same name in different scopes will not be changed.
2018-12-17 05:08:41 +08:00
2019-06-05 04:07:06 +08:00
- 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.
2018-12-17 05:08:41 +08:00
2019-06-05 04:07:06 +08:00
- 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.
2018-12-17 05:08:41 +08:00
The exporting rules when creating or updating a variable are identical to the scoping rules for variables:
2019-06-05 04:07:06 +08:00
- Variables may be explicitly set to either exported or not exported. When an exported variable goes out of scope, it is unexported.
2018-12-17 05:08:41 +08:00
2019-06-05 04:07:06 +08:00
- 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.
2018-12-17 05:08:41 +08:00
2019-06-05 04:07:06 +08:00
- 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.
2018-12-17 05:08:41 +08:00
2021-11-27 01:29:10 +08:00
In query mode, the scope to be examined can be specified. Whether the variable has to be a path variable or exported can also be specified.
2018-12-17 05:08:41 +08:00
2019-05-05 20:01:07 +08:00
In erase mode, if variable indices are specified, only the specified slices of the list variable will be erased.
2018-12-17 05:08:41 +08:00
2022-03-12 00:18:41 +08:00
`` 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 :envvar: `flags` to '-l', not making the variable local.
2018-12-17 05:08:41 +08:00
2020-11-20 23:07:22 +08:00
Exit status
-----------
2022-03-12 00:18:41 +08:00
In assignment mode, `` set `` does not modify the exit status, but passes along whatever :envvar: `status` was set, including by command substitutions. This allows capturing the output and exit status of a subcommand, like in `` if set output (command) `` .
2020-11-20 23:07:22 +08:00
In query mode, the exit status is the number of variables that were not found.
2020-11-20 23:09:02 +08:00
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 any of the variables did not exist or was a :ref: `special read-only variable <variables-special>` .
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Examples
2019-01-03 12:10:47 +08:00
--------
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
# Prints all global, exported variables.
set -xg
2019-09-17 16:50:52 +08:00
2018-12-19 11:14:04 +08:00
# Sets the value of the variable $foo to be 'hi'.
set foo hi
2019-09-17 16:50:52 +08:00
2018-12-19 11:14:04 +08:00
# Appends the value "there" to the variable $foo.
set -a foo there
2019-09-17 16:50:52 +08:00
2018-12-19 11:14:04 +08:00
# Does the same thing as the previous two commands the way it would be done pre-fish 3.0.
set foo hi
set foo $foo there
2019-09-17 16:50:52 +08:00
2018-12-19 11:14:04 +08:00
# Removes the variable $smurf
set -e smurf
2019-09-17 16:50:52 +08:00
2019-05-05 20:01:07 +08:00
# Changes the fourth element of the $PATH list to ~/bin
2018-12-19 11:14:04 +08:00
set PATH[4] ~/bin
2019-09-17 16:50:52 +08:00
2018-12-20 04:02:45 +08:00
# Outputs the path to Python if `` type -p `` returns true.
2018-12-19 11:14:04 +08:00
if set python_path (type -p python)
echo "Python is at $python_path"
end
2018-12-17 05:08:41 +08:00
2020-11-20 23:07:22 +08:00
# Setting a variable doesn't modify $status!
false
set foo bar
echo $status # prints 1, because of the "false" above.
true
set foo banana (false)
echo $status # prints 1, because of the "(false)" above.
2021-02-02 15:35:38 +08:00
# Like other shells, pass a variable to just one command:
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 09:13:29 +08:00
# Run fish with a temporary home directory.
HOME=(mktemp -d) fish
# Which is essentially the same as:
begin; set -lx HOME (mktemp -d); fish; end
2018-12-19 10:44:30 +08:00
Notes
2019-01-03 12:10:47 +08:00
-----
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
Fish versions prior to 3.0 supported the syntax `` set PATH[1] PATH[4] /bin /sbin `` , which worked like
`` set PATH[1 4] /bin /sbin `` . This syntax was not widely used, and was ambiguous and inconsistent.