2016-02-04 22:37:45 -05:00
|
|
|
|
Bash to Xonsh Translation Guide
|
|
|
|
|
================================
|
2016-02-21 16:25:16 -05:00
|
|
|
|
As you have probably figured out by now, xonsh is not ``sh``-lang compliant.
|
2016-02-04 22:37:45 -05:00
|
|
|
|
If your muscles have memorized all of the Bash prestidigitations, this page
|
2017-06-07 11:51:06 -04:00
|
|
|
|
will help you put a finger on how to do the equivalent task in xonsh.
|
2016-02-04 22:37:45 -05:00
|
|
|
|
|
2019-01-29 13:44:49 +03:00
|
|
|
|
For shell scripts, the recommended file extension is ``xsh``, and shebang
|
|
|
|
|
line is ``#!/usr/bin/env xonsh``.
|
|
|
|
|
|
2016-05-07 13:40:55 -04:00
|
|
|
|
.. list-table::
|
2016-02-04 22:37:45 -05:00
|
|
|
|
:widths: 30 30 40
|
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
|
|
* - Bash
|
|
|
|
|
- Xonsh
|
|
|
|
|
- Notes
|
2022-07-01 21:17:01 +05:30
|
|
|
|
* - ``echo --arg="val"``
|
|
|
|
|
|
2021-02-22 18:28:36 +03:00
|
|
|
|
``echo {}``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
|
2021-02-22 18:28:36 +03:00
|
|
|
|
``echo \;``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
|
2021-02-22 18:28:36 +03:00
|
|
|
|
- ``echo --arg "val"``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
|
2021-02-22 18:28:36 +03:00
|
|
|
|
``echo "{}"``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
|
2021-02-22 18:28:36 +03:00
|
|
|
|
``echo ";"``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
|
|
|
|
|
- Read `Subprocess Strings <https://xon.sh/tutorial_subproc_strings.html>`_ tutorial
|
|
|
|
|
to understand how strings become arguments in xonsh.
|
2021-02-22 18:28:36 +03:00
|
|
|
|
There is no notion of an escaping character in xonsh like the backslash (``\``) in bash.
|
2022-07-01 21:17:01 +05:30
|
|
|
|
Single or double quotes can be used to remove the special meaning of certain
|
2021-02-22 18:28:36 +03:00
|
|
|
|
characters, words or brackets.
|
2024-05-02 17:48:25 +02:00
|
|
|
|
* - ``IFS``
|
|
|
|
|
- ``$XONSH_SUBPROC_OUTPUT_FORMAT``
|
|
|
|
|
- Changing the output representation and splitting.
|
2016-02-04 22:37:45 -05:00
|
|
|
|
* - ``$NAME`` or ``${NAME}``
|
|
|
|
|
- ``$NAME``
|
|
|
|
|
- Look up an environment variable by name.
|
2018-12-19 00:07:20 +11:00
|
|
|
|
* - ``export NAME=Peter``
|
|
|
|
|
- ``$NAME = 'Peter'``
|
2020-05-27 09:31:44 +03:00
|
|
|
|
- Setting an environment variable. See also :ref:`$UPDATE_OS_ENVIRON <update_os_environ>`.
|
2018-12-19 00:07:20 +11:00
|
|
|
|
* - ``unset NAME``
|
|
|
|
|
- ``del $NAME``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
- Unsetting/deleting an environment variable.
|
2016-06-07 23:19:31 -04:00
|
|
|
|
* - ``echo "$HOME/hello"``
|
|
|
|
|
- ``echo "$HOME/hello"``
|
|
|
|
|
- Construct an argument using an environment variable.
|
2016-06-04 17:20:06 -05:00
|
|
|
|
* - ``something/$SOME_VAR/$(some_command)``
|
|
|
|
|
- ``@('something/' + $SOME_VAR + $(some_command).strip())``
|
|
|
|
|
- Concatenate a variable or text with the result of running a command.
|
2020-10-13 12:26:03 -04:00
|
|
|
|
* - ``echo 'my home is $HOME'``
|
|
|
|
|
- ``echo @("my home is $HOME")``
|
|
|
|
|
- Escape an environment variable from expansion.
|
2016-06-07 23:19:31 -04:00
|
|
|
|
* - ``${!VAR}``
|
2016-02-04 22:37:45 -05:00
|
|
|
|
- ``${var or expr}``
|
2016-05-07 13:40:55 -04:00
|
|
|
|
- Look up an environment variable via another variable name. In xonsh,
|
2016-02-04 22:37:45 -05:00
|
|
|
|
this may be any valid expression.
|
2020-09-25 03:14:12 +03:00
|
|
|
|
* - ``ENV1=VAL1 command``
|
2021-02-22 18:28:36 +03:00
|
|
|
|
- ``$ENV1=VAL1 command``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
|
2021-02-22 18:28:36 +03:00
|
|
|
|
or ``with ${...}.swap(ENV1=VAL1): command``
|
2020-09-25 02:57:41 +03:00
|
|
|
|
- Set temporary environment variable(s) and execute the command.
|
|
|
|
|
Use the second notation with an indented block to execute many commands in the same context.
|
2020-09-25 03:11:50 +03:00
|
|
|
|
* - ``alias ll='ls -la'``
|
|
|
|
|
- ``aliases['ll'] = 'ls -la'``
|
|
|
|
|
- Alias in xonsh could be a subprocess command as a string or list of arguments or any Python function.
|
2016-02-04 22:37:45 -05:00
|
|
|
|
* - ``$(cmd args)`` or ```cmd args```
|
2016-05-20 19:55:56 -04:00
|
|
|
|
- ``@$(cmd args)``
|
|
|
|
|
- Command substitution (allow the output of a command to replace the
|
|
|
|
|
command itself). Tokenizes and executes the output of a subprocess
|
|
|
|
|
command as another subprocess.
|
2020-09-25 02:50:22 +03:00
|
|
|
|
* - ``v=`echo 1```
|
|
|
|
|
- ``v=$(echo 1)``
|
2020-09-25 02:57:41 +03:00
|
|
|
|
- In bash, backticks mean to run a captured subprocess - it's ``$()`` in xonsh. Backticks in xonsh
|
2020-09-25 02:50:22 +03:00
|
|
|
|
mean regex globbing (i.e. ``ls `/etc/pass.*```).
|
2020-10-06 00:15:20 +03:00
|
|
|
|
* - ``echo -e "\033[0;31mRed text\033[0m"``
|
2020-10-06 00:09:12 +03:00
|
|
|
|
- ``printx("{RED}Red text{RESET}")``
|
|
|
|
|
- Print colored text as easy as possible.
|
2020-09-25 02:57:41 +03:00
|
|
|
|
* - ``shopt -s dotglob``
|
|
|
|
|
- ``$DOTGLOB = True``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
- Globbing files with ``*`` or ``**`` will also match dotfiles, or those ‘hidden’ files whose names
|
2020-09-25 03:04:40 +03:00
|
|
|
|
begin with a literal `.`. Such files are filtered out by default like in bash.
|
2020-09-25 02:57:41 +03:00
|
|
|
|
* - ``if [ -f "$FILE" ];``
|
|
|
|
|
- ``p'/path/to/file'.exists()`` or ``pf'{file}'.exists()``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
- Path objects can be instantiated and checked directly using p-string syntax.
|
2016-02-04 22:37:45 -05:00
|
|
|
|
* - ``set -e``
|
|
|
|
|
- ``$RAISE_SUBPROC_ERROR = True``
|
2016-05-07 13:40:55 -04:00
|
|
|
|
- Cause a failure after a non-zero return code. Xonsh will raise a
|
2016-02-04 22:37:45 -05:00
|
|
|
|
``supbrocess.CalledProcessError``.
|
|
|
|
|
* - ``set -x``
|
2020-05-01 03:18:28 +03:00
|
|
|
|
- ``trace on`` and ``$XONSH_TRACE_SUBPROC = True``
|
2016-02-04 22:37:45 -05:00
|
|
|
|
- Turns on tracing of source code lines during execution.
|
2016-02-10 03:22:30 -05:00
|
|
|
|
* - ``&&``
|
2020-09-25 02:57:41 +03:00
|
|
|
|
- ``&&`` or ``and``
|
2016-02-10 03:22:30 -05:00
|
|
|
|
- Logical-and operator for subprocesses.
|
|
|
|
|
* - ``||``
|
2020-09-25 02:57:41 +03:00
|
|
|
|
- ``||`` as well as ``or``
|
2016-02-10 03:22:30 -05:00
|
|
|
|
- Logical-or operator for subprocesses.
|
2020-10-15 19:47:48 +03:00
|
|
|
|
* - ``$$``
|
|
|
|
|
- ``os.getpid()``
|
|
|
|
|
- Get PID of the current shell.
|
2016-05-07 13:40:55 -04:00
|
|
|
|
* - ``$?``
|
2024-05-22 15:25:35 +02:00
|
|
|
|
- ``__xonsh__.last.rtn`` anywhere or ``_.rtn`` in prompt mode
|
2022-07-01 21:17:01 +05:30
|
|
|
|
- Returns the exit code, or status, of the previous command. The underscore ``_`` is working
|
|
|
|
|
in the prompt mode. To get the exit code of the command in xonsh script
|
2024-05-22 15:25:35 +02:00
|
|
|
|
use ``!().rtn`` for not interactive processes.
|
2016-09-02 13:33:45 +03:00
|
|
|
|
* - ``!$``
|
2018-09-30 13:59:08 -07:00
|
|
|
|
- ``__xonsh__.history[-1, -1]``
|
2016-09-02 13:33:45 +03:00
|
|
|
|
- Get the last argument of the last command
|
2017-10-09 12:09:29 -04:00
|
|
|
|
* - ``$<n>``
|
|
|
|
|
- ``$ARG<n>``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
- Command line argument at index ``n``,
|
2017-10-09 12:09:29 -04:00
|
|
|
|
so ``$ARG1`` is the equivalent of ``$1``.
|
|
|
|
|
* - ``$@``
|
|
|
|
|
- ``$ARGS``
|
|
|
|
|
- List of all command line argument and parameter strings.
|
2021-02-22 18:28:36 +03:00
|
|
|
|
* - ``while getopts``
|
2024-05-22 15:25:35 +02:00
|
|
|
|
- Use `argparse <https://docs.python.org/3/library/argparse.html>`_ or `click <https://click.palletsprojects.com>`_.
|
|
|
|
|
- See also `awesome-cli-app <https://github.com/anki-code/xonsh-awesome-cli-app>`_ and
|
|
|
|
|
`xontrib-argcomplete <https://github.com/anki-code/xontrib-argcomplete>`_ .
|
2020-09-25 03:31:30 +03:00
|
|
|
|
* - ``complete``
|
|
|
|
|
- ``completer list``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
- As with many other shells, xonsh ships with the ability to complete partially-specified arguments
|
2020-09-25 10:20:04 +03:00
|
|
|
|
upon hitting the “tab” key.
|
|
|
|
|
* - OhMyBash or BashIt
|
2024-05-22 15:25:35 +02:00
|
|
|
|
- `awesome-xontribs <https://github.com/xonsh/awesome-xontribs>`_
|
2022-07-01 21:17:01 +05:30
|
|
|
|
- Xontributions, or ``xontribs``, are a set of tools and conventions for extending the functionality
|
2020-09-25 10:20:04 +03:00
|
|
|
|
of xonsh beyond what is provided by default.
|
2020-05-27 09:31:44 +03:00
|
|
|
|
* - Display completions as list
|
|
|
|
|
- ``$COMPLETIONS_DISPLAY = 'readline'``
|
|
|
|
|
- Display completions will emulate the behavior of readline.
|
2020-09-25 10:27:14 +03:00
|
|
|
|
* - ``docker run -it bash``
|
|
|
|
|
- ``docker run -it xonsh/xonsh:slim``
|
2022-07-01 21:17:01 +05:30
|
|
|
|
- Xonsh publishes a handful of containers, primarily targeting CI and automation use cases.
|
2020-09-25 10:27:14 +03:00
|
|
|
|
All of them are published on `Docker Hub <https://hub.docker.com/u/xonsh>`_.
|
2020-10-01 01:36:08 +03:00
|
|
|
|
* - ``exit 1``
|
|
|
|
|
- ``exit(1)``
|
2020-05-12 17:37:15 +03:00
|
|
|
|
- Exiting from the current script.
|
2020-05-01 03:18:28 +03:00
|
|
|
|
|
|
|
|
|
To understand how xonsh executes the subprocess commands try
|
|
|
|
|
to set :ref:`$XONSH_TRACE_SUBPROC <xonsh_trace_subproc>` to ``True``:
|
|
|
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
|
|
>>> $XONSH_TRACE_SUBPROC = True
|
|
|
|
|
>>> echo $(echo @('hello')) @('wor' + 'ld') | grep hello
|
|
|
|
|
TRACE SUBPROC: (['echo', 'hello'],)
|
|
|
|
|
TRACE SUBPROC: (['echo', 'hello\n', 'world'], '|', ['grep', 'hello'])
|
|
|
|
|
|
2022-07-01 21:17:01 +05:30
|
|
|
|
If after time you still try to type ``export``, ``unset`` or ``!!`` commands
|
2023-03-17 13:25:22 +06:00
|
|
|
|
there are the `bashisms <https://github.com/xonsh/xontrib-bashisms>`_
|
|
|
|
|
and `sh <https://github.com/anki-code/xontrib-sh>`_ xontribs.
|