- Notifications
You must be signed in to change notification settings - Fork4
Paste and run commands from bash, zsh, fish, tcsh in xonsh shell.
License
anki-code/xontrib-sh
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Paste and run commands from bash, fish, zsh, tcsh inxonsh shell.
If you like the idea click ⭐ on the repo and andtweet.
xpip install -U xontrib-shecho'xontrib load sh'>>~/.xonshrc# Reload xonsh
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 inthe 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 modifythe 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']# defaultxontribloadsh!echohello
Also you can set the shell explicitly i.e.!bash
or!b
(the first letter of the shell).
The main use case ofxontrib-sh
is when you copy and paste the sh-commands from some article or instructionand 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 ofcurl
:
TMP=/tmp&&cd$TMP&& ( [[-x$(command -v curl) ]]&&echo"Yes"||echo"No" )
You hesitatehow 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.
!echo 01.{05..10}# Or explicitly:!becho 01.{05..10}!bashecho 01.{05..10}
bash:01.05 01.06 01.07 01.08 01.09 01.10
!foriin 1 2 3doecho$idone
bash:123
$ENV='hello'!echo $ENV!
bash:hello!
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']xontribloadsh# Run fish command:!set-Uvar1value1# bash: line 0: set: -U: invalid option
To avoid this use the explicit setting the shell i.e.!fish set -U var1 value1
.
Also, sincepwsh andcmd 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 isone shell in$XONTRIB_SH_SHELLS
Why it's better thanxonsh subprocess macros?
Xonsh subprocess macros is not supporting multiline commands and require more keystrokes.
$XONTRIB_SH_USEFULL
(defaultTrue
) - enables to set the shell explicitly by the name of the shell i.e.!bash
.$XONTRIB_SH_USEFIRST
(defaultTrue
) - enables to set the shell explicitly by the first letter i.e.!b
instead of!bash
.
- This package is the part ofrc-awesome - awesome snippets of code for xonshrc in xonsh shell.
- This package is the part ofergopack - the pack of ergonomic xontribs.
- This package was created withxontrib cookiecutter template.
- How to create a xonsh alias that have a behavior like in xontrib-sh?