简体   繁体   English

什么是Bash文件扩展名?

[英]What is the Bash file extension?

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? 我在文本编辑器中编写了一个bash脚本,我将脚本保存为什么扩展名,因为它可以作为bash脚本运行? I've created a script that should in theory start an ssh server. 我创建了一个理论上应该启动ssh服务器的脚本。 I am wondering how to make the script execute once I click on it. 我想知道如何在我点击它之后使脚本执行。 I am running OS X 10.9.5. 我正在运行OS X 10.9.5。

Disagreeing with the other answers, there's a common convention to use a .sh extension for shell scripts -- but it's not a useful convention. 不同意其他答案,对于shell脚本使用.sh扩展名有一个共同的约定 - 但它不是一个有用的约定。 It's better not to use an extension at all. 最好不要使用扩展名。 The advantage of being able tell that foo.sh is a shell script because of its name is minimal, and you pay for it with a loss of flexibility. 能够告诉foo.sh是一个shell脚本的优点是因为它的名字很小,而且你为它付出了代价而失去了灵活性。

To make a bash script executable, it needs to have a shebang line at the top: 要使bash脚本可执行,它需要在顶部有一个shebang行:

#!/bin/bash

and use the chmod +x command so that the system recognizes it as an executable file. 并使用chmod +x命令,以便系统将其识别为可执行文件。 It then needs to be installed in one of the directories listed in your $PATH . 然后需要将其安装在$PATH列出的其中一个目录中。 If the script is called foo , you can then execute it from a shell prompt by typing foo . 如果脚本名为foo ,则可以通过键入foo从shell提示符执行它。 Or if it's in the current directory (common for temporary scripts), you can type ./foo . 或者,如果它位于当前目录中(临时脚本通用),则可以键入./foo

Neither the shell nor the operating system pays any attention to the extension part of the file name. shell和操作系统都不会关注文件名的扩展部分。 It's just part of the name. 这只是名字的一部分。 And by not giving 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. 通过给它一个特殊的扩展,你确保使用它的任何人(用户或另一个脚本)不必关心它是如何实现的,无论它是一个shell脚本(sh,bash,csh还是其他) ,Perl,Python或Awk脚本,或二进制可执行文件。 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. 系统经过专门设计,可以调用解释的脚本或二进制可执行文件,而无需了解或了解它是如何实现的。

UNIX-like systems started out with a purely textual command-line interface. 类UNIX系统最初只使用纯文本命令行界面。 GUIs like KDE and Gnome were added later. 稍后添加了像KDE和Gnome这样的GUI。 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. 在GUI桌面系统中,您通常可以运行程序(再次,无论是脚本还是二进制可执行文件),例如,双击引用它的图标。 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. 它比从shell提示符运行它灵活得多。 But for some programs (mostly GUI clients) it can be more convenient. 但对于某些程序(主要是GUI客户端),它可以更方便。

Shell scripting is best learned from the command line, not from a GUI. Shell脚本最好从命令行学习,而不是从GUI学习。

(Some tools do pay attention to file extensions. For example, compilers typically use the extension to determine the language the code is written in: .c for C, .cpp for 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. 请记住,UNIX(和类UNIX系统)不是Windows。 MS Windows generally uses a file's extension to determine how to open/execute it. MS Windows通常使用文件的扩展名来确定如何打开/执行它。 Binary executables need to have a .exe extension. 二进制可执行文件需要具有.exe扩展名。 If you have a UNIX-like shell installed under Windows, you can configure Windows to recognize a .sh extension as a shell script, and use the shell to open it; 如果在Windows下安装了类UNIX的shell,则可以将Windows配置为将.sh扩展名识别为shell脚本,并使用shell打开它; Windows doesn't have the #! Windows没有#! convention. 惯例。

You don't need any extension (or you could choose an arbitrary one, but .sh is 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 . 你应该用#!/bin/bashexecve(2) syscall理解第一行)开始你的脚本,你应该通过chmod u+x使你的文件可执行。 so if your script is in some file $HOME/somedir/somescriptname.sh you need to type once 因此,如果您的脚本位于某个文件$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 ~/.bashrc if your login shell is bash ) 除非您输入整个文件路径,否则应该将该文件放在PATH提到的某个目录中(参见environ(7)execvp(3) ),如果您的登录shell是,则可以在~/.bashrc永久设置该目录。 bash

BTW, you could write your script in some other language, eg in Python by starting it with #!/usr/bin/python , or in Ocaml by starting it with #!/usr/bin/ocaml ... 顺便说一句,您可以用其他语言编写脚本,例如用#!/usr/bin/python开始编写#!/usr/bin/python ,或者用#!/usr/bin/ocaml开始编写Ocaml ...

Executing your script by double-clicking (on what? you did not say!) is a desktop environment issue and could be desktop specific (might be different with Kde, Mate, Gnome, .... or IceWM or RatPoison). 通过双击(关于你没说的内容!)执行你的脚本是一个桌面环境问题,可能是桌面特定的(可能与Kde,Mate,Gnome,....或IceWM或RatPoison不同)。 Perhaps reading EWMH spec might help you getting a better picture. 也许阅读EWMH规范可能会帮助您获得更好的图片。

Perhaps making your script executable with chmod might make it clickable on your desktop (apparently, Quartz on MacOSX). 也许使用chmod使脚本可执行可能会使它在桌面上可点击(显然,MacOSX上的Quartz )。 But then you probably should make it give some visual feedback. 但是你可能应该让它给出一些视觉反馈。

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. 我不相信通过单击运行shell脚本是个好主意。 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. 您可能希望能够为shell脚本提供参数(以及如何通过单击?来执行此操作),并且您应该关心它的输出。 If you are able to write a shell script, you are able to use an interactive shell in a terminal. 如果您能够编写shell脚本,则可以在终端中使用交互式shell。 That it the best and most natural way to use a script. 这是使用脚本的最佳和最自然的方式。 Good interactive shells (eg zsh or fish or perhaps a recent bash ) have delicious and configurable autocompletion facilities and you won't have to type a lot (learn to use the tab key of your keyboard). 良好的交互式shell(例如zshfish或者最近的bash )具有美味且可配置的自动完成功能,您不必输入很多(学习使用键盘的Tab键)。 Also, scripts and programs are often parts of composite commands (pipelines, etc...). 此外,脚本和程序通常是复合命令(管道等)的一部分。

PS. PS。 I'm using Unix since 1986, and Linux since 1993. I never started my own programs or scripts by clicking. 我从1986年开始使用Unix,自1993年开始使用Linux。我从未通过点击开始自己的程序或脚本。 Why should I? 我为什么要?

just .sh . 只是.sh

Run the script like this: 像这样运行脚本:

./script.sh

EDIT: Like anubhava said, the extension doesn't really matter. 编辑:像anubhava所说,扩展并不重要。 But for organisational reasons, it is still recommended to use extensions. 但出于组织原因,仍建议使用扩展。

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 .command extension. 如果您在Mac上并且希望能够通过双击运行脚本,则需要使用.command扩展名。 Also same as before make file executable with chmod -x . 与使用chmod -x生成文件可执行文件之前相同。

As was noted before, this isn't really that useful tbh. 如前所述,这并不是真正有用的。

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. TL; DR - 如果脚本的用户(不一定是开发人员)使用GUI界面,则取决于他们使用的文件浏览器。 MacOS's Finder will require the .sh extension in order to execute the script. MacOS的Finder将需要.sh扩展名才能执行脚本。 Gnome Nautilus, however, recognizes properly shebanged scripts with or without the .sh extension. 但是,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 .sh extension on your bash scripts. 如果您是跳入和跳出bash并且一般使用终端或正在为不使用终端的其他人开发工具的类型,请在bash脚本上添加.sh扩展名。 That way, users of that script have the option of double-clicking on that file in a GUI file browser to run the script. 这样,该脚本的用户可以选择在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. 如果您是主要在终端中完成所有或大部分工作的类型,请不要在bash脚本上添加任何扩展名。 They would serve no purpose in the terminal, assuming that you've already set up your ~/.bashrc file to visually differentiate scripts from directories. 它们在终端中没有任何用处,假设您已经设置了~/.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/bash on the first line. 第一行上带有#!/bin/bash无扩展名的文件。

    It worked by double-clicking on the file. 它通过双击文件来工作。

  2. A file with a .sh extension with #!/bin/bash on the first line. 第一行带有#!/bin/bash扩展名为.sh的文件。

    It worked by double-clicking on the file. 它通过双击文件来工作。

  3. A file with NO extension with NO #!/bin/bash on the first line. 没有扩展名的文件,第一行没有#!/bin/bash

    It worked by double-clicking on the file...technically, but the GUI gave no indication that it was a shell script. 它通过双击文件...在技术上工作,但GUI没有表明它是一个shell脚本。 It said it was just a plain text file. 它说它只是一个纯文本文件。

  4. A file with a .sh extension with NO #!/bin/bash on the first line. 扩展名为.sh的文件,第一行没有#!/bin/bash

    It worked by double-clicking on the file. 它通过双击文件来工作。

However, as Keith Thompson, in the comments of this answer, wisely pointed out, relying on the using the .sh extension instead of the bash shebang on the first line of the file ( #!/bin/bash ) it could cause problems. 然而,正如Keith Thompson在这个答案的评论中明智地指出的那样,依赖于在文件的第一行使用.sh扩展而不是bash shebang( #!/bin/bash ),它可能会导致问题。

Another however, I recall when I was previously using MacOS, that even properly shebanged (is that a word?) bash scripts without a .sh extension could not be run from the GUI on MacOS. 另一个,我记得当我以前使用MacOS时,即使是正确的shebanged(是一个单词吗?)没有.sh扩展名的bash脚本也无法从MacOS上的GUI运行。 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 .sh extension matters. 如果这是真的,那将证明在.sh扩展很重要的地方至少有一个文件浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM