什么是 Bash 文件扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27813563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:49:38  来源:igfitidea点击:

What is the Bash file extension?

bashshell

提问by Amedeo

I have written a bash script in a text editor, what extension do I save my script as so it can run as a bash script? I've created a script that should in theory start an ssh server. I am wondering how to make the script execute once I click on it. I am running OS X 10.9.5.

我已经在文本编辑器中编写了一个 bash 脚本,我应该保存什么扩展名以便它可以作为 bash 脚本运行?我创建了一个理论上应该启动 ssh 服务器的脚本。我想知道一旦我点击它,如何让脚本执行。我正在运行 OS X 10.9.5。

回答by Keith Thompson

Disagreeing with the other answers, there's a common convention to use a .shextension for shell scripts -- but it's not a useful convention. It's better not to use an extension at all. The advantage of being able tell that foo.shis a shell script because of its name is minimal, and you pay for it with a loss of flexibility.

不同意其他答案,使用.shshell 脚本的扩展有一个共同的约定——但这不是一个有用的约定。最好不要使用扩展。foo.sh由于它的名字,能够分辨它是一个 shell 脚本的好处是最小的,并且你为它付出了灵活性的损失。

To make a bash script executable, it needs to have a shebangline at the top:

要使 bash 脚本可执行,它需要在顶部有一个shebang行:

#!/bin/bash

and use the chmod +xcommand so that the system recognizes it as an executable file. It then needs to be installed in one of the directories listed in your $PATH. If the script is called foo, you can then execute it from a shell prompt by typing foo. Or if it's in the current directory (common for temporary scripts), you can type ./foo.

并使用该chmod +x命令使系统将其识别为可执行文件。然后需要将其安装在$PATH. 如果调用了该脚本,则foo可以在 shell 提示符下键入foo. 或者如果它在当前目录中(对于临时脚本很常见),您可以输入./foo.

Neither the shell nor the operating system pays any attention to the extension part of the file name. It's just part of the name. And by notgiving it a special extension, you ensure that anyone (either a user or another script) that uses it doesn't have to care how it was implemented, whether it's a shell script (sh, bash, csh, or whatever), a Perl, Python, or Awk script, or a binary executable. The system is specifically designed so that either an interpreted script or a binary executable can be invoked without knowing or caring how it's implemented.

外壳程序和操作系统都不会注意文件名的扩展名部分。这只是名字的一部分。通过给它一个特殊的扩展名,你可以确保使用它的任何人(用户或其他脚本)不必关心它是如何实现的,无论它是一个 shell 脚本(sh、bash、csh 还是其他) 、Perl、Python 或 Awk 脚本,或二进制可执行文件。该系统经过专门设计,因此可以在不知道或不关心其实现方式的情况下调用解释脚本或二进制可执行文件。

UNIX-like systems started out with a purely textual command-line interface. GUIs like KDE and Gnome were added later. In a GUI desktop system, you can typically run a program (again, whether it's a script or a binary executable) by, for example, double-clicking on an icon that refers to it. Typically this discards any output the program might print and doesn't let you pass command-line arguments; it's much less flexible than running it from a shell prompt. But for some programs (mostly GUI clients) it can be more convenient.

类 UNIX 系统从纯文本命令行界面开始。后来添加了像 KDE 和 Gnome 这样的 GUI。在 GUI 桌面系统中,您通常可以通过例如双击引用它的图标来运行程序(同样,无论它是脚本还是二进制可执行文件)。通常,这会丢弃程序可能打印的任何输出,并且不允许您传递命令行参数;它比从 shell 提示运行它灵活得多。但是对于某些程序(主要是 GUI 客户端),它可能更方便。

Shell scripting is best learned from the command line, not from a GUI.

Shell 脚本最好从命令行学习,而不是从 GUI 学习。

(Some tools dopay attention to file extensions. For example, compilers typically use the extension to determine the language the code is written in: .cfor C, .cppfor c++, etc. This convention doesn't apply to executable files.)

(有些工具确实会注意文件扩展名。例如,编译器通常使用扩展名来确定代码的编写语言:.c对于 C、.cpp对于 C++ 等。此约定不适用于可执行文件。)

Keep in mind that UNIX (and UNIX-like systems) are not Windows. MS Windows generally uses a file's extension to determine how to open/execute it. Binary executables need to have a .exeextension. If you have a UNIX-like shell installed under Windows, you can configure Windows to recognize a .shextension as a shell script, and use the shell to open it; Windows doesn't have the #!convention.

请记住,UNIX(和类 UNIX 系统)不是 Windows。MS Windows 通常使用文件的扩展名来确定如何打开/执行它。二进制可执行文件需要有.exe扩展名。如果你在 Windows 下安装了类 UNIX 的 shell,你可以将 Windows 配置为将.sh扩展识别为 shell 脚本,并使用 shell 来打开它;Windows 没有#!约定。

回答by Basile Starynkevitch

You don't need any extension (or you could choose an arbitrary one, but .shis a useful convention).

您不需要任何扩展名(或者您可以选择任意一个,但这.sh是一个有用的约定)。

You should start your script with #!/bin/bash(that first line is understood by execve(2)syscall), and you should make your file executable by chmod u+x. so if your script is in some file $HOME/somedir/somescriptname.shyou need to type once

你应该用#!/bin/bash(第一行被execve(2)系统调用理解开始你的脚本,你应该让你的文件通过chmod u+x. 所以如果你的脚本在某个文件中,$HOME/somedir/somescriptname.sh你需要输入一次

 chmod u+x  $HOME/somedir/somescriptname.sh

in a terminal. See chmod(1)for the command and chmod(2)for the syscall.

在一个终端。有关命令,请参阅chmod(1 ),有关系统调用,请参阅chmod( 2)

Unless you are typing the whole file path, you should put that file in some directory mentioned in your PATH(see environ(7)& execvp(3)), which you might set permanently in your ~/.bashrcif your login shell is bash)

除非您输入整个文件路径,否则您应该将该文件放在您提到的某个目录中PATH(请参阅environ(7)execvp(3)),~/.bashrc如果您的登录 shell 是,您可能会在您的目录中永久设置bash

BTW, you could write your script in some other language, e.g. in Python by starting it with #!/usr/bin/python, or in Ocaml by starting it with #!/usr/bin/ocaml...

顺便说一句,您可以用其他语言编写脚本,例如在 Python 中以 开头#!/usr/bin/python,或在 Ocaml 中以#!/usr/bin/ocaml...

Executing your script by double-clicking (on what? you did not say!) is a desktop environmentissue and could be desktop specific (might be different with Kde, Mate, Gnome, .... or IceWM or RatPoison). Perhaps reading EWMHspec might help you getting a better picture.

通过双击(在什么上?你没有说!)执行你的脚本是一个桌面环境问题,可能是桌面特定的(可能与 Kde、Mate、Gnome、......或 IceWM 或 RatPoison 不同)。也许阅读EWMH规范可能会帮助您获得更好的了解。

Perhaps making your script executable with chmodmight make it clickable on your desktop (apparently, Quartzon MacOSX). But then you probably should make it give some visual feedback.

也许使您的脚本可执行chmod可能会使其在您的桌面上可点击(显然,MacOSX 上的Quartz)。但是你可能应该让它提供一些视觉反馈。

And several computers don't have any desktop, including your own when you access it remotely with ssh.

并且几台计算机没有任何桌面,包括您自己的,当您使用ssh远程访问它时。

I don't believe it is a good idea to run your shell script by clicking. You probably want to be able to give arguments to your shell script (and how would you do that by clicking?), and you should care about its output. If you are able to write a shell script, you are able to use an interactive shell in a terminal. That it the best and most natural way to use a script. Good interactive shells (e.g. zshor fishor perhaps a recent bash) have delicious and configurable autocompletionfacilities and you won't have to type a lot (learn to use the tabkey of your keyboard). Also, scripts and programs are often parts of composite commands (pipelines, etc...).

我认为通过单击运行 shell 脚本不是一个好主意。您可能希望能够为您的 shell 脚本提供参数(以及您将如何通过单击来做到这一点?),并且您应该关心它的输出。如果您能够编写 shell 脚本,则可以在终端中使用交互式 shell。这是使用脚本的最佳和最自然的方式。良好的交互式 shell(例如zshfish或者可能是最近的bash)具有美味且可配置的自动完成功能,并且您不必键入很多内容(学会使用tab键盘上的键)。此外,脚本和程序通常是复合命令(管道等)的一部分。

PS. I'm using Unix since 1986, and Linux since 1993. I never started my own programs or scripts by clicking. Why should I?

附注。我从 1986 年开始使用 Unix,从 1993 年开始使用 Linux。我从来没有通过单击来启动自己的程序或脚本。我为什么要?

回答by w_jay

I know this is quite old now but I feel like this adds to what the question was asking for.

我知道这现在已经很老了,但我觉得这增加了问题的要求。

If your on a mac and you want to be able to run a script by double clicking it you need to use the .commandextension. Also same as before make file executable with chmod -x.

如果您使用的是 Mac 并且希望能够通过双击运行脚本,则您需要使用该.command扩展程序。也与之前相同,使用chmod -x.

As was noted before, this isn't really that useful tbh.

如前所述,这并不是真正有用的 tbh。

回答by Ryan Hart

TL;DR -- If the user (not necessarily the developer) of the script is using a GUI interface, it depends on what file browser they are using. MacOS's Finder will require the .shextension in order to execute the script. Gnome Nautilus, however, recognizes properly shebanged scripts with or without the .shextension.

TL;DR -- 如果脚本的用户(不一定是开发人员)使用 GUI 界面,这取决于他们使用的文件浏览器。MacOS 的 Finder 需要.sh扩展才能执行脚本。然而,Gnome Nautilus 可以正确识别带有或不带有.sh扩展名的shebanged 脚本。

I know it's already been said multiple times the reasons for and against using an extension on bash scripts, but not as much why or why not to use extensions, but I have what I consider to be a good rule of thumb.

我知道在 bash 脚本上使用扩展的原因和反对的原因已经多次说过,但没有那么多为什么或为什么不使用扩展,但我认为这是一个很好的经验法则。

If you're the type who hops in and out of bash and using the terminal in general or are developing a tool for someone else who does not use the terminal, put a .shextension on your bash scripts. That way, users of that script have the option of double-clicking on that file in a GUI file browser to run the script.

如果您是那种经常进出 bash 并通常使用终端的类型,或者正在为不使用终端的其他人开发工具,请.sh在您的 bash 脚本中添加扩展名。这样,该脚本的用户可以选择在 GUI 文件浏览器中双击该文件来运行该脚本。

If you're the type who primarily does all or most of your work in the terminal, don't bother putting any extension on your bash scripts. They would serve no purpose in the terminal, assuming that you've already set up your ~/.bashrcfile to visually differentiate scripts from directories.

如果您是主要在终端中完成所有或大部分工作的类型,请不要在 bash 脚本上添加任何扩展。它们在终端中没有任何作用,假设您已经设置了~/.bashrc文件以在视觉上区分脚本和目录。

Edit:

编辑:

In the Gnome Nautilus file browser with 4 test files (each with permissions given for the file to be executed) with stupidly simple bash command to open a terminal window (gnome-terminal):

在 Gnome Nautilus 文件浏览器中,有 4 个测试文件(每个文件都有执行文件的权限),使用非常简单的 bash 命令打开终端窗口 ( gnome-terminal):

  1. A file with NO extension with #!/bin/bashon the first line.

    It worked by double-clicking on the file.

  2. A file with a .shextension with #!/bin/bashon the first line.

    It worked by double-clicking on the file.

  3. A file with NO extension with NO #!/bin/bashon the first line.

    It worked by double-clicking on the file...technically, but the GUI gave no indication that it was a shell script. It said it was just a plain text file.

  4. A file with a .shextension with NO #!/bin/bashon the first line.

    It worked by double-clicking on the file.

  1. #!/bin/bash第一行没有扩展名的文件。

    它通过双击文件来工作。

  2. 第一行带有.sh扩展名的文件#!/bin/bash

    它通过双击文件来工作。

  3. #!/bin/bash第一行没有扩展名的文件。

    它通过双击文件来工作......技术上,但 GUI 没有表明它是一个 shell 脚本。它说这只是一个纯文本文件。

  4. 第一行.sh带有 NO 扩展名的文件#!/bin/bash

    它通过双击文件来工作。

However, as Keith Thompson, in the comments of this answer, wisely pointed out, relying on the using the .shextension instead of the bash shebang on the first line of the file (#!/bin/bash) it could cause problems.

但是,正如 Keith Thompson 在此答案的评论中明智地指出的那样,依赖于.sh在文件 ( #!/bin/bash)的第一行使用扩展名而不是 bash shebang可能会导致问题。

Another however, I recall when I was previously using MacOS, that even properly shebanged (is that a word?) bash scripts without a .shextension could not be run from the GUI on MacOS. I would love for someone to correct me on that in the comments though. If this is true, it would prove that there is a least one file browser out there where the .shextension matters.

然而,另一个我记得当我以前使用 MacOS 时,即使是正确的 shebanged(这是一个词?)没有.sh扩展名的bash 脚本也无法从 MacOS 上的 GUI 运行。不过,我希望有人在评论中纠正我。如果这是真的,就证明至少有一个文件浏览器在那里.sh扩展很重要。

回答by Marc Anton Dahmen

just .sh.

只是.sh

Run the script like this:

像这样运行脚本:

./script.sh

EDIT: Like anubhava said, the extension doesn't really matter. But for organisational reasons, it is still recommended to use extensions.

编辑:就像 anubhava 所说的,扩展名并不重要。但出于组织原因,仍然建议使用扩展。