Bash to Xonsh Translation Guide¶
Xonsh relies primarily on the Python syntax and is not a suitable replacement for sh and is thus considered a non-POSIX shell. This page provides xonsh equivalents for common patterns in Bash.
Bash |
Xonsh |
Notes |
---|---|---|
No special object for represent session. |
|
The |
|
|
The recommended file extension is |
|
|
Use |
|
|
Read Subprocess Strings tutorial
to understand how strings become arguments in xonsh.
There is no notion of an escaping character in xonsh like the backslash ( |
|
|
Changing the output representation and splitting. Also take a look into |
|
|
Look up an environment variable by name. |
|
|
Setting an environment variable. See also $UPDATE_OS_ENVIRON. |
|
|
Unsetting/deleting an environment variable. |
|
|
Construct an argument using an environment variable. |
|
|
Concatenate a variable or text with the result of running a command. |
|
|
Escape an environment variable from expansion. |
|
|
Look up an environment variable via another variable name. In xonsh, this may be any valid expression. |
|
or |
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. |
|
|
Alias in xonsh could be a subprocess command as a string or list of arguments or any Python function. |
|
|
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. |
|
|
In bash, backticks mean to run a captured subprocess - it’s |
|
|
Print colored text as easy as possible. |
|
|
Globbing files with |
|
|
Path objects can be instantiated and checked directly using p-string syntax. |
|
|
Cause a failure after a non-zero return code. Xonsh will raise a
|
|
|
Turns on tracing of source code lines during execution. |
|
|
Logical-and operator for subprocesses. |
|
|
Logical-or operator for subprocesses. |
|
|
Get PID of the current shell. |
|
|
Returns the exit code, or status, of the previous command. The underscore |
|
|
Get the last argument of the last command |
|
|
Command line argument at index |
|
|
List of all command line argument and parameter strings. |
|
See also awesome-cli-app and xontrib-argcomplete . |
|
|
|
As with many other shells, xonsh ships with the ability to complete partially-specified arguments upon hitting the “tab” key. |
OhMyBash or BashIt |
Xontributions, or |
|
Display completions as list |
|
Display completions will emulate the behavior of readline. |
|
|
Xonsh publishes a handful of containers, primarily targeting CI and automation use cases. All of them are published on Docker Hub. |
|
|
Exiting from the current script. |
To understand how xonsh executes the subprocess commands try
to set $XONSH_TRACE_SUBPROC to True
:
>>> $XONSH_TRACE_SUBPROC = True
>>> echo $(echo @('hello')) @('wor' + 'ld') | grep hello
TRACE SUBPROC: (['echo', 'hello'],)
TRACE SUBPROC: (['echo', 'hello\n', 'world'], '|', ['grep', 'hello'])
If after time you still try to type export
, unset
or !!
commands
there are the bashisms
and sh xontribs.