.. historic nodename: Invocation

Invocation
================================================================================
.. index:: single: invocation; Command

Invocation
--------------------------------------------------------------------------------
.. index:: single: shell options; Command

.. index:: single: options, shell; Command

.. index:: single: shell flags; Command

.. index:: single: flags, shell; Command

The following flags are interpreted by the shell when invoked to determine
where the shell will read commands from:

``-c``
    
    Take the first argument as a command to execute, rather than reading commands
    from a script or standard input.  If any further arguments are given, the
    first one is assigned to ``$0``, rather than being used as a positional
    parameter.
    

``-i``
    
    Force shell to be interactive.  It is still possible to specify a
    script to execute.
    

``-s``
    
    Force shell to read commands from the standard input.
    If the ``-s`` flag is not present and an argument is given,
    the first argument is taken to be the pathname of a script to
    execute.
    

If there are any remaining arguments after option processing, and neither
of the options ``-c`` or ``-s`` was supplied, the first argument is taken
as the file name of a script containing shell commands to be executed.  If
the option ``PATH_SCRIPT`` is set, and the file name does not contain a
directory path (i.e. there is no ``/`` in the name), first the current
directory and then the command path given by the variable ``PATH`` are
searched for the script.  If the option is not set or the file name
contains a ``/`` it is used directly.

After the first one or two arguments have been appropriated as described above,
the remaining arguments are assigned to the positional parameters.

For further options, which are common to invocation and the ``set``
builtin, see :manpage:`zshoptions`.

The long option ``--emulate`` followed (in a separate word) by an
emulation mode may be passed to the shell.
The emulation modes are those described for the ``emulate`` builtin,
see :manpage:`zshbuiltins`.
The ``--emulate`` option must precede any other options (which might
otherwise be overridden), but following options are honoured, so
may be used to modify the requested emulation mode.  Note that certain
extra steps are taken to ensure a smooth emulation when this option
is used compared with the ``emulate`` command within the shell: for
example, variables that conflict with POSIX usage such as ``path`` are
not defined within the shell.

Options may be specified by name using the ``-o`` option.  ``-o`` acts like
a single-letter option, but takes a following string as the option name.
For example,

.. code-block:: zsh
   :class: example

    zsh -x -o shwordsplit scr

runs the script ``scr``, setting the ``XTRACE`` option by the corresponding
letter ``-x`` and the ``SH_WORD_SPLIT`` option by name.
Options may be turned :emphasis:`off` by name by using ``+o`` instead of ``-o``.
``-o`` can be stacked up with preceding single-letter options, so for example
``-xo shwordsplit`` or ``-xoshwordsplit`` is equivalent to
``-x -o shwordsplit``.

.. index:: single: long option; Command

Options may also be specified by name in GNU long option style,
``--``:var:`option-name`.  When this is done, ``-`` characters in the
option name are permitted: they are translated into ``_``, and thus ignored.
So, for example, ``zsh --sh-word-split`` invokes zsh with the
``SH_WORD_SPLIT`` option turned on.  Like other option syntaxes, options can
be turned off by replacing the initial ``-`` with a ``+``; thus
``+-sh-word-split`` is equivalent to ``--no-sh-word-split``.
Unlike other option syntaxes, GNU-style long options cannot be stacked with
any other options, so for example ``-x-shwordsplit`` is an error,
rather than being treated like ``-x --shwordsplit``.

.. index:: single: --version; Command

.. index:: single: --help; Command

The special GNU-style option ``--version`` is handled; it sends to
standard output the shell's version information, then exits successfully.
``--help`` is also handled; it sends to standard output a list of
options that can be used when invoking the shell, then exits successfully.

Option processing may be finished, allowing following arguments that start with
``-`` or ``+`` to be treated as normal arguments, in two ways.
Firstly, a lone ``-`` (or ``+``) as an argument by itself ends
option processing.  Secondly, a special option ``--`` (or
``+-``), which may be specified on its own (which is the standard
POSIX usage) or may be stacked with preceding options (so ``-x-`` is
equivalent to ``-x --``).  Options are not permitted to be stacked
after ``--`` (so ``-x-f`` is an error), but note the GNU-style
option form discussed above, where ``--shwordsplit`` is permitted
and does not end option processing.

Except when the :strong:`sh`/:strong:`ksh` emulation single-letter options are in effect,
the option ``-b`` (or ``+b``) ends option processing.
``-b`` is like ``--``, except that further single-letter options
can be stacked after the ``-b`` and will take effect as normal.

.. include:: compat.rst

