mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-22 13:13:15 +08:00
docs: Remove "Variable scope for functions" chapter
This didn't need to be separately. Also rename "More on universal variables" because it's the chapter on universal variables.
This commit is contained in:
parent
e84f588d11
commit
dda0c8178d
|
@ -1086,6 +1086,24 @@ Here is an example of local vs function-scoped variables::
|
|||
# Will output Sir Terry's wisdom.
|
||||
end
|
||||
|
||||
When a function calls another, local variables aren't visible::
|
||||
|
||||
function shiver
|
||||
set phrase 'Shiver me timbers'
|
||||
end
|
||||
|
||||
function avast
|
||||
set --local phrase 'Avast, mateys'
|
||||
# Calling the shiver function here can not
|
||||
# change any variables in the local scope
|
||||
# so phrase remains as we set it here.
|
||||
shiver
|
||||
echo $phrase
|
||||
end
|
||||
avast
|
||||
|
||||
# Outputs "Avast, mateys"
|
||||
|
||||
When in doubt, use function-scoped variables. When you need to make a variable accessible everywhere, make it global. When you need to persistently store configuration, make it universal. When you want to use a variable only in a short block, make it local.
|
||||
|
||||
.. _variables-override:
|
||||
|
@ -1121,8 +1139,8 @@ This syntax is supported since fish 3.1.
|
|||
|
||||
.. _variables-universal:
|
||||
|
||||
More on universal variables
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
Universal Variables
|
||||
^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Universal variables are variables that are shared between all the user's fish sessions on the computer. Fish stores many of its configuration options as universal variables. This means that in order to change fish settings, all you have to do is change the variable value once, and it will be automatically updated for all sessions, and preserved across computer reboots and login/logout.
|
||||
|
||||
|
@ -1132,30 +1150,6 @@ To see universal variables in action, start two fish sessions side by side, and
|
|||
|
||||
Do not append to universal variables in :ref:`config.fish <configuration>`, because these variables will then get longer with each new shell instance. Instead, simply set them once at the command line.
|
||||
|
||||
.. _variables-functions:
|
||||
|
||||
Variable scope for functions
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
When calling a function, all current local variables temporarily disappear. This shadowing of the local scope is needed since the variable namespace would become cluttered, making it very easy to accidentally overwrite variables from another function.
|
||||
|
||||
For example::
|
||||
|
||||
function shiver
|
||||
set phrase 'Shiver me timbers'
|
||||
end
|
||||
|
||||
function avast
|
||||
set --local phrase 'Avast, mateys'
|
||||
# Calling the shiver function here can not
|
||||
# change any variables in the local scope
|
||||
shiver
|
||||
echo $phrase
|
||||
end
|
||||
avast
|
||||
|
||||
# Outputs "Avast, mateys"
|
||||
|
||||
.. _variables-export:
|
||||
|
||||
Exporting variables
|
||||
|
|
Loading…
Reference in New Issue
Block a user