bash脚本的开始 [英] Starting of bash script

查看:105
本文介绍了bash脚本的开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是背后的原因写

#! /bin/bash

在最开始的每bash脚本的?

at the very starting of every bash script ?

我的手段,我知道这是一种技巧,但我想知道它是如何工作的究竟?

I means, I know that it is kind of hack but I want to know how it exactly works ?

推荐答案

如果你调用脚本使用显式间preTER,像

If you call the script with an explicit interpreter, like

 bash foo.sh
 /bin/bash foo.sh
 dash foo.sh
 sh foo.sh

您choosen间preTER时,无论家当说什么,这仅仅是一个注释。

your choosen interpreter is used, no matter what the shebang says, which is just a comment.

如果你使脚本可执行文件,并启动它:

If you make the script executable, and start it:

 chmod a+x foo.sh
 ./foo.sh

内核查找家当,开始与该所指明间preTER程序。由于内核不知道路径,全路径必须给予,或 ENV 已被使用:

 #!/usr/bin/env python 

选择某个程序的本地路径。

to choose the local path for a program.

由于冲刺时,bash,zsh中等等都是不完全兼容,您应该调用该程序没有一个具体的跨preTER,因为作者应该知道他做了什么。

Since dash, bash, zsh and so on aren't fully compatible, you should call the program without a specific interpreter, because the author should have known what he does.

也许你的系统上的 SH /斌/庆典,但要注意的链接!外壳可以反省它是如何调用,和bash可以在某种程度上,被称为POSIX模式,这是略有不同的,只有什么它一般不会一个子集行事。

Maybe on your system sh is a link to /bin/bash, but beware! The shell can introspect how it was called, and bash can act in a way, called POSIX mode, which is slightly different and only a subset of what it normally does.

如果你写的安装或服务器脚本,这可能会在不同的平台上运行 - 想OSX,Linux和Solaris等,不同的炮弹可用,您会尝试自己限制这个子集,以获得兼容,可重复使用的结果。

If you write scripts for installation or for servers, which might run on different platforms - think OsX, Linux, Solaris and so on, where different shells are available, you will try to restrict yourself to this subset, to gain a compatible, reusable result.

不过,您的私人机器上,你可能preFER像的zsh 庆典更方便炮弹。

But on your private machine, you might prefer the more convenient shells like zsh and bash.

这篇关于bash脚本的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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