https://github.com/bouk/babelfish
Translate bash scripts to fish
https://github.com/bouk/babelfish
Last synced: about 1 year ago
JSON representation
Translate bash scripts to fish
- Host: GitHub
- URL: https://github.com/bouk/babelfish
- Owner: bouk
- License: mit
- Created: 2020-06-07T21:27:58.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-27T19:25:11.000Z (over 1 year ago)
- Last Synced: 2025-04-11T14:20:28.258Z (about 1 year ago)
- Language: Go
- Size: 93.8 KB
- Stars: 202
- Watchers: 3
- Forks: 6
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome - bouk/babelfish
README
# babelfish
Translate bash scripts to [fish](https://fishshell.com).
## Why?
Because I got annoyed by having to use [fish-foreign-env](https://github.com/oh-my-fish/plugin-foreign-env) or [bass](https://github.com/edc/bass), which are slow, since they create multiple bash processes. With this program I can translate bash scripts to fish, and run them directly in fish.
## But how?
`babelfish` parses the script using [mvdan.cc/sh](https://github.com/mvdan/sh), and then translates bash expressions to the equivalent fish code. That's it! You can find the code that walks the AST and emits fish code [here](https://github.com/bouk/babelfish/blob/master/translate/translate.go).
## Install
If you have Homebrew, just run:
```shell
brew install babelfish
```
Else:
```shell
go install bou.ke/babelfish@latest
```
## Example
```sh
# Pass some code on stdin to translate it
$ echo 'f() { export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket); local cool=yep; }' | babelfish
function f
set -gx SSH_AUTH_SOCK (gpgconf --list-dirs agent-ssh-socket | string collect; or echo)
set -l cool 'yep'
end
# Pass the result to source to load it into fish
$ echo 'echo Nice to meet you user $UID' | babelfish | source
Nice to meet you user 502
# Or install the shell hook!
$ source babel.fish
$ source chruby.sh
$ chruby
ruby-2.5
ruby-2.6
ruby-2.7
```
## To do
Probably still a lot. There's a couple variables like `$BASH_SOURCE` that aren't translated, and not all arithmetic expressions are implemented either. Pull requests and issues welcome!