docs: Add something on variables-as-commands

Specifically point towards the necessary splitting (as always,
separate ahead of time) and the keyword thing.

Fixes #9797
This commit is contained in:
Fabian Boehm 2023-05-21 10:13:54 +02:00
parent aac30367bf
commit b435fc4539
2 changed files with 24 additions and 0 deletions

View file

@ -15,6 +15,8 @@ Description
**command** forces the shell to execute the program *COMMANDNAME* and ignore any functions or builtins with the same name.
In ``command foo``, ``command`` is a keyword.
The following options are available:
**-a** or **--all**

View file

@ -747,6 +747,28 @@ Some more examples::
# The second element of every variable, so output is
# 2 5
Variables as command
''''''''''''''''''''
Like other shells, you can run the value of a variable as a command.
::
> set -g EDITOR emacs
> $EDITOR foo # opens emacs, possibly the GUI version
If you want to give the command an argument inside the variable it needs to be a separate element::
> set EDITOR emacs -nw
> $EDITOR foo # opens emacs in the terminal even if the GUI is installed
> set EDITOR "emacs -nw"
> $EDITOR foo # tries to find a command called "emacs -nw"
Also like other shells, this only works with commands, builtins and functions - it will not work with keywords because they have syntactical importance.
For instance ``set if $if`` won't allow you to make an if-block, and ``set cmd command`` won't allow you to use the :cmds:`command <command>` decorator, but only uses like ``$cmd -q foo``.
.. _expand-command-substitution:
Command substitution