返回资讯中心
外部精选
软件工程
#开发者实时流

Using Env(1) in the Shebang

Some of you out there may have tried to pass flags to a script that was being

Hacker News 最新ykgoon2026/10/27 · 今天3 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

Some of you out there may have tried to pass flags to a script that was being invoked via /usr/bin/env in the shebang (#!), such as python. You might recall an error such as:

/usr/bin/env: python -d: No such file or directory

This error is super annoying, so I went about trying to figure out how I can pass arguments to python (or even things like ipython or bpython).

The idea is we can abuse the concept of a polygot to shim in some things we care about.

Let’s take a look at a quick script I hacked up to use bpython with a pre-made script that drops into interactive work.

#!/bin/sh """": exec /usr/bin/env bpython -i $0 $@ """ import hy print "Hython is now importable!"

Let’s step through this slowly. First, the bits the bash sees:

#!/bin/sh """": exec /usr/bin/env bpython -i $0 $@

Which will cause bpython to reload the file, which looks like the following to Python:

#!/bin/sh """": exec /usr/bin/env bpython -i $0 $@ """ import hy print "Hython is now importable!"

Where Python can now ignore the docstring. Magic!

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →