Skip to main content

Paste and run commands from bash, fish, zsh, tcsh in xonsh shell.

If you like the idea click ⭐ on the repo and and tweet.

Install

xpip install -U xontrib-sh
echo 'xontrib load sh' >> ~/.xonshrc
# Reload xonsh

Usage

Start the line with ! (exclamation mark with space) then paste the sh-compatible commands after it and run. The commands syntax will be tested in the shells from list (if installed) and the commands will be run in the first matching shell. By default list of shells contains bash and sh.

The commands will be executed in the environment that will be inherited from current but if the commands modify the environment there will no changes in source xonsh environment.

To set the list of shells use environment variable before loading the xontrib:

$XONTRIB_SH_SHELLS = ['bash', 'sh']  # default
xontrib load sh
! echo hello

Also you can set the shell explicitly i.e. !bash or !b (the first letter of the shell).

The main use case

The main use case of xontrib-sh is when you copy and paste the sh-commands from some article or instruction and this commands are environment agnostic and you want to run it without rewriting it on xonsh or run sh-shell.

For example you've found snippet of bash commands that check existing of curl:

TMP=/tmp && cd $TMP && ( [[ -x $(command -v curl) ]] && echo "Yes" || echo "No" )  

You hesitate how xonsh will execute this and you're absolutely right there will be syntax error. To run this just start with ! or !b or !bash and paste the commands. As result you'll see the right message.

Examples

Bash brace expansion

! echo 01.{05..10}
# Or explicitly:
!b echo 01.{05..10}
!bash echo 01.{05..10}
bash:
01.05 01.06 01.07 01.08 01.09 01.10

Multiline loop

! for i in 1 2 3
do
   echo $i
done
bash:
1
2
3

Use environment variables to pass values from xonsh to sh

$ENV = 'hello'
! echo $ENV!
bash:
hello!

Known issues

Determining the shell on short command

In case of usage many different shells the detection of the shell works perfect when the commands contain shell-specific syntax. But if you run the short command that could be valid in all shells the first matched shell will be chosen but it's could be wrong.

For example you have bash and fish in the list of shells. The short fish command may be determined as bash command. As result the command will be failed:

$XONTRIB_SH_SHELLS = ['bash', 'fish']
xontrib load sh
# Run fish command:
! set -U var1 value1
# bash: line 0: set: -U: invalid option

To avoid this use the explicit setting the shell i.e. !fish set -U var1 value1.

Also, since pwsh and cmd shells don't have an option to detect their own syntax, they can only be invoked:

  • explicitly by their name, i.e. !p or !pwsh
  • implicitly via the ! prefix only when there is one shell in $XONTRIB_SH_SHELLS

Why it's better than xonsh subprocess macros?

Xonsh subprocess macros is not supporting multiline commands and require more keystrokes.

Additional options

  • $XONTRIB_SH_USEFULL (default True) - enables to set the shell explicitly by the name of the shell i.e. !bash .
  • $XONTRIB_SH_USEFIRST (default True) - enables to set the shell explicitly by the first letter i.e. !b instead of !bash .

Metadata

Release files for xontrib-sh 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xontrib-sh 0.3.2
File Size Uploaded Provenance Actions
xontrib_sh-0.3.2.tar.gz 5.3 kB Without provenance Details for xontrib_sh-0.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xontrib-sh 0.3.2
File Interpreter ABI Platform Actions
xontrib_sh-0.3.2-py3-none-any.whl Python 3 none any Details for xontrib_sh-0.3.2-py3-none-any.whl

Total release size: 10.6 kB

Release files / xontrib_sh-0.3.2.tar.gz

Download URL xontrib_sh-0.3.2.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d352ab13092fd3581ffded7fd0c36fbec98b3115997fff0a60036520c5104a1a
BLAKE2b-256 checksum
How to use checksums
7dd21800b15a6cf0f7e5979d6cb31edf1982b292a8f157b85c61abf39644af48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / xontrib_sh-0.3.2-py3-none-any.whl

Download URL xontrib_sh-0.3.2-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b7e6656e5a031dc03dc1269a6cb20ef67b71aa52f81defac5ced890136a7cef5
BLAKE2b-256 checksum
How to use checksums
d9602656ad4055adc13071fc1c0714a92b65fd4d071ba21bc5d57f77458f363a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for xontrib-sh 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page