Bash to Xonsh Translation Guide¶
As you have probably figured out by now, xonsh is not sh
-lang compliant.
If your muscles have memorized all of the Bash prestidigitations, this page
will help you put a finger on how to do the equivalent task in xonsh.
For shell scripts, the recommended file extension is xsh
, and shebang
line is #!/usr/bin/env xonsh
.
Bash |
Xonsh |
Notes |
---|---|---|
|
|
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. |
|
|
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.