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 |
---|---|---|
|
|
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. |
|
|
Look up an environment variable via another variable name. In xonsh, this may be any valid expression. |
|
|
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. |
|
|
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. |
|
|
Returns the exit code, or status, of the previous command. |
|
|
Set temporary environment variable(s) and execute for command. Use an indented block to execute many commands in the same context. |
|
|
Get the last argument of the last command |
|
|
Command line argument at index |
|
|
List of all command line argument and parameter strings. |
|
|
Globbing files with “*” or “**” will also match dotfiles, or those ‘hidden’ files whose names begin with a literal ‘.’. Such files are filtered out by default like in bash. |
Display completions as list |
|
Display completions will emulate the behavior of readline. |
|
|
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'])