究竟是什么ENV Bash中呢? [英] Exactly what does env do in Bash?

查看:137
本文介绍了究竟是什么ENV Bash中呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用bash的时候(在Cygwin下)获得这个行为:

I get this behavior when using Bash (under Cygwin):

$ printf '\u00d5'
\u00d5
$ env printf '\u00d5' # This results in the behavior I want
Õ

如果我用UTF-8或ISO-8859-1编码在终端不要紧。

It does not matter if I use UTF-8 or ISO-8859-1 encoding in the terminal.

我的问题是:究竟是什么 ENV 吗?我为什么需要它在这种特殊情况下?

My questions are: Exactly what does env do? Why do I need it in this specific case?

推荐答案

ENV 未在庆典,这是一个独立的可执行文件,它是用来设置或运行程序之前明确环境变量。在特定情况下,它的运行二进制的printf 而不是shell内建。可以通过使用绝对路径实现相同的结果:

env is not in bash, it's a standalone executable, it is used to set or clear environment variable before running program. In your particular case, it's running the binary printf instead of the shell builtin. You can achieve the same result by using the absolute path:

/usr/bin/printf '\u00d5'


侵入性最小的方法可能是以下几点:重新定义的printf 功能,并具有猛砸处理其余部分。来源,包含以下文件:


The least intrusive method is perhaps the following: redefine the printf function and have Bash handle the rest. Source a file that contains the following:

function printf()
{
  $(which printf) "$@"
}

或作为一个班轮函数printf(){$(其中的printf)$ @; } 。当然你可以替换 $(其中的printf)的/ usr / bin中/ printf的 ...

or as a one-liner function printf() { $(which printf) "$@"; }. Of course you could replace the $(which printf) for a /usr/bin/printf ...

然后,只需用printf因为你已经习惯了。您的脚本保持不变,你甚至可以介绍一个条件来定义仅在某些版本的Bash功能。

Then simply use printf as you're used to. Your script stays the same and you could even introduce a condition to define the function only on certain Bash versions.

据我所知,你也可以离开了函数,但我觉得它增加了可读性。

AFAIK you can also leave out the function, but I find it adds to readability.

通常情况下, ENV 也用于脚本的哈希邦线的努力移植。原因是, ENV 几乎总是在的/ usr / bin中/ env的,而庆典不是总是在 /斌/庆典尽可能多的散列砰线暗示。例如:

Commonly, env is also used in the hash-bang lines of scripts that strive to be portable. The reason being that env is nearly always at /usr/bin/env, while bash isn't always at /bin/bash as many a hash-bang line implies. Example:

#!/usr/bin/env bash

也适用于其他程序/间preters:

also works for other programs/interpreters:

#!/usr/bin/env python

这篇关于究竟是什么ENV Bash中呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆