# \`#!\` shebangs in \`%sh\`

**URL:** <https://discuss.kakoune.com/t/shebangs-in-sh/1800>\
**Category:** Recipes & Guides\
**Tags:** scripting, language\
**Created:** [June 29, 2021, 4:02pm UTC](https://discuss.kakoune.com/t/shebangs-in-sh/1800 "2021-06-29T16:02:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![almr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/almr/32/934_2.png) [@almr](https://discuss.kakoune.com/u/almr)\
**Post date:** [June 29, 2021, 4:02pm UTC](https://discuss.kakoune.com/t/shebangs-in-sh/1800/1 "2021-06-29T16:02:41Z")

</div>

I have this in my `kakrc`:

```auto
def tst -params .. %{
  echo -debug %sh{#!perl -wn
    print if /:root:/
  }
}
tst /etc/passwd

```

which produces the expected line in `*debug*`

### How?

- get [`fakesh.sh`](https://gitlab.com/kstr0k/f8ksh/-/blob/master/fakesh.sh) from [`f8ksh`](https://gitlab.com/kstr0k/f8ksh) on GitLab
- run `kak` with `KAKOUNE_POSIX_SHELL=~/bin/fakesh.sh kak`

### Again, how?

The script parses the command line

- if it starts with a shebang, it runs the body below the shebang with the specified shebang, passing on arguments. Unlike a shebang, binaries can be in `$PATH`, not just absolute names.
- if not, it `eval`’s the body (no `fork / exec`), again setting up `"$@"` properly

### What next

I see a lot of possibilities; for example, I could have a server that keeps pre-forked interpreters (`perl`, `lua`, whatever) in stdin mode; when `fakesh` tells the server to run something, it simply sends of a `source` command to the appropriate interpreter (and forks a new one).

Perhaps this has been done already, I haven’t reviewed everything in the ecosystem. Anyway, hope this is helpful.

> **[kstr0k / f8ksh · GitLab](https://gitlab.com/kstr0k/f8ksh)**
>
> POSIX shell logging / profiling wrapper (optional: obeys in-script shebangs)
