[转帖]为什么需要在脚本文件的开头加上#!/ bin / bash?
本文翻译自:Why do you need to put #!/bin/bash at the beginning of a script file?
I have made Bash scripts before and they all ran fine without this at the beginning. 之前我已经制作了Bash脚本,并且在开始时它们都运行良好。 What's the point of putting it in? 把它放进去有什么意义? Would things be any different? 会有什么不同吗?
Also, how do you pronounce # ? 另外,你怎么发音# ? I know that ! 我知道! is pronounced as "bang." 发音为“bang”。
How is #! 怎么#! pronounced? 发音?
#1楼
参考:https://stackoom.com/question/bcxa/为什么需要在脚本文件的开头加上-bin-bash
#2楼
It can be useful to someone that uses a different system that does not have that library readily available. 对于使用不具有该库的不同系统的人来说,它可能是有用的。 If that is not declared and you have some functions in your script that are not supported by that system, you should declare #/bin/bash. 如果未声明并且您的脚本中有一些该系统不支持的函数,则应声明#/ bin / bash。 I've ran into this problem before at work and now I just include it as a practice. 我在工作之前遇到过这个问题,现在我只是把它作为一种练习。
#3楼
Also you will see some other parameters after #!/bin/bash, for example 例如,您还会在#!/ bin / bash之后看到一些其他参数 #!/bin/bash -v -x
read this to get more idea. 阅读本文以获得更多想法。
https://unix.stackexchange.com/questions/124272/what-do-the-arguments-v-and-x-mean-to-bash . https://unix.stackexchange.com/questions/124272/what-do-the-arguments-v-and-x-mean-to-bash 。
#4楼
It's a convention so the *nix shell knows what kind of interpreter to run. 这是一个约定,所以* nix shell知道要运行什么样的解释器。
For example, older flavors of ATT defaulted to sh (the Bourne shell), while older versions of BSD defaulted to csh (the C shell). 例如,较旧版本的ATT默认为sh (Bourne shell),而较旧版本的BSD默认为csh (C shell)。
Even today (where most systems run bash, the "Bourne Again Shell" ), scripts can be in bash, python, perl, ruby, PHP, etc, etc. For example, you might see #!/bin/perl or #!/bin/perl5 . 即使在今天(大多数系统运行bash, “Bourne Again Shell” ),脚本也可以是bash,python,perl,ruby,PHP等等。例如,你可能会看到#!/bin/perl或#!/bin/perl5 。
PS: The exclamation mark ( ! ) is affectionately called "bang" . PS:感叹号( ! )被亲切地称为“爆炸” 。 The shell comment symbol ( # ) is sometimes called "hash" . shell注释符号( # )有时称为“哈希” 。
PPS: Remember - under *nix, associating a suffix with a file type is merely a convention , not a "rule" . PPS:记住 - 在* nix下,将后缀与文件类型相关联仅仅是一种约定 ,而不是“规则” 。 An executable can be a binary program, any one of a million script types and other things as well. 可执行文件可以是二进制程序,也可以是一百万种脚本类型中的任何一种。 Hence the need for #!/bin/bash . 因此需要#!/bin/bash 。
#5楼
The operating system takes default shell to run your shell script. 操作系统使用默认shell来运行shell脚本。 so mentioning shell path at the beginning of script, you are asking the OS to use that particular shell. 所以在脚本开头提到shell路径,你要求OS使用那个特定的shell。 It is also useful for portability . 它对于便携性也很有用。
#6楼
Every distribution has a default shell. 每个发行版都有一个默认shell。 Bash is the default on the majority of the systems. Bash是大多数系统的默认设置。 If you happen to work on a system that has a different default shell, then the scripts might not work as intended if they are written specific for Bash. 如果您碰巧在具有不同默认shell的系统上工作,那么如果脚本是针对Bash编写的,则脚本可能无法正常工作。
Bash has evolved over the years taking code from ksh and sh . 多年来,Bash从ksh和sh获取代码。
Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. 添加#!/bin/bash作为脚本的第一行,告诉操作系统调用指定的shell来执行脚本中的命令。
#! is often referred to as a "hash-bang", "she-bang" or "sha-bang". 通常被称为“哈希砰”,“嘻嘻”或“沙梆”。
[转帖]为什么需要在脚本文件的开头加上#!/ bin / bash?的更多相关文章
- linux shell脚本中的开头#!/bin/bash的含义
对于linux上需要执行 的shell脚本,通常第一行的内容是 #!/bin/bash 当然有很多时候不规范的写法可以忽略掉这一句,执行起来好像也是ok,结果没什么不一样 .. 这只是因为在我们常用 ...
- 在caffe中执行脚本文件时 报错:-bash: ./train.sh: Permission denied
报错原因:没有权限 解决方法:chmod 777 train.sh获得权限
- Shell中 调用/引用/包含 另外的脚本文件的两种方法
脚本 first (测试示例1) #!/bin/bash echo 'your are in first file' 问)在当前脚本文件中调用另外一个脚本文件? 方法一: 使用 source 脚本 s ...
- shell脚本--文件包含
首先介绍一下shell中包含文件的方法,在C,C++,PHP中都是用include来包含文件,Go和Java使用import来包含(导入)包,而在shell中,很简单,只需要一个点“.”,然后跟着文件 ...
- tecplot 脚本文件
一个简单的C++代码,可以生成tecplot的脚本文件,打开文件就能自动绘图.绘图过程是先把文件导入,然后镜像,最后生成一个动画,但是导入的文件名称要求是有规律的. /* ** This progra ...
- Maven打包生成可运行bat/sh脚本文件
利用Maven的appassembler-maven-plugin插件,就可以实现自动打包可运行的脚本,还可以跨平台. <plugin> <groupId>org ...
- 获取当前正在执行的Javascript脚本文件的路径
获取当前JavaScript脚本文件的路径,在特定场景下可能需要,比如写模块加载器,或者进行日志记录.下面这段脚本适用于所有浏览器来获取正在执行js文件的路径,但是该方法只适用于脚本加载过程中执行的情 ...
- MS SQL执行大脚本文件时,提示“内存不足”的解决办法()
问题描述: 当客户服务器不允许直接备份时,往往通过导出数据库脚本的方式来部署-还原数据库, 但是当数据库导出脚本很大,用Microsoft SQL Server Management Studio执行 ...
- SqlCommand执行带GO的SQL脚本文件
今天工作中遇到了这个问题,其实只要把GO替换成“;”就行了,其它人写的例子用Split来拆分这一个脚本文件的内容,完全没有必要.希望对你有用.
- linux执行sh脚本文件命令
linux执行sh脚本文件命令 很多时候需要多个命令来完成一项工作,而这个工作又常常是重复的,这个时候我们自然会想到将这些命令写成sh脚本,下次执行下这个脚本一切就都搞定了,下面就是发布代码的一个脚本 ...
随机推荐
- 痞子衡嵌入式:在i.MXRT1170上快速点亮一款全新LCD屏的方法与步骤(MIPI DSI接口)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是在i.MXRT1170上快速点亮一款全新LCD屏的方法与步骤. 我们知道 LCD 屏的接口有很多:DPI-RGB.MIPI DSI.DB ...
- javaGUI入门之swing(一)
javaGUI入门之swing(一) 前面学过javafx,发现他们有"异曲同工"之处,demo写起来也类似,无非是类名不一样.个人觉得实现一个桌面应用不应该只看用一种语言一种框架 ...
- [Luogu 4912 帕秋莉的魔法] 题解报告
算法:DP, 背包,动态规划 简化版题目: 给定 \(n\) 个物品,物品的价值为 \(v_1 - v_n\),物品的体积为 \(w_1 - w_n\).需要选择一些物品,使它们的体积和为 \(V\) ...
- 普通用户登录切换到root用户
使用su命令: 在终端中输入以下命令并按Enter键: su - 输入root用户的密码,然后按Enter键. 如果密码正确,你将会切换为root用户,并且可以执行root用户的操作. 使用sudo命 ...
- WMTS地图服务每一层级分辨率
目录 1. 概述 2. 详论 2.1. Web墨卡托 2.2. 大地经纬度 3. 参考 1. 概述 WMTS地图服务每一层级的分辨率是多少?关于这个问题以前推算过,但总是忘记了.网上查询又是一堆废话, ...
- Java 插入、隐藏/显示、删除Excel行或列
概述 操作Excel工作表时,对表格中的行或列数据可执行,包括插入.隐藏.显示.删除等在内的多种操作需求,本文将通过Java代码示例演示每种操作的具体实现方法.文中方法使用了Java Excel类库( ...
- 探秘华为云盘古大模型:AI for industries的身体力行
摘要:大模型是新一轮AI发展的核心,其已在推进产业智能化升级中已表现出巨大潜力,并将在未来三年里形成风起云涌之势. 本文分享自华为云社区<探秘华为云盘古大模型:AI for industries ...
- AI开发效率低,你可以试试华为NAIE AutoML
摘要:为解决AI工程师在开发AI应用场景所遇到的问题,NAIE平台落地AutoML框架(工具)来辅助大家更高效.更迅速解决AI开发问题. 你是不是还在为掌握的AI算法少而烦恼? 你是不是还在为选择某个 ...
- 超详细教程:SpringBoot整合MybatisPlus
摘要:本文为大家带来SpringBoot整合MybatisPlus的教程,实现SpringBoot项目中依赖数据模块进行数据操作,并进行简单测试. 本文分享自华为云社区<SpringBoot整合 ...
- 资深Linux 系统管理员常用的15个很好用的Cron工作示例
摘要:Linux Cron 实用程序是一种在特定时间和/或日期持续调度例行后台作业的有效方法.这篇文章讲述了15 个很好用的 crontab 工作调度示例. 本文分享自华为云社区<Linux C ...