IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Bash shebang (井号和叹号) -> 正文阅读

[系统运维]Bash shebang (井号和叹号)

Bash shebang ( #! )

1. Bash

The Bourne shell (sh) is a shell command-line interpreter for computer operating systems.

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell (sh).
Bash 是一个命令处理器,通常运行于文本窗口中,并能执行用户直接输入的命令。

The shell’s name is an acronym for Bourne Again Shell, a pun on the name of the Bourne shell (sh) that it replaces and the notion of being “born again”.
Bash 是 Bourne shell 的后继兼容版本与开放源代码版本,它的名称来自 Bourne shell (sh) 的一个双关语 Bourne Again SHell。

pun [p?n]:n. 双关语 vi. 使用双关语

操作系统支持 Shell 的类型:

(base) yongqiang@yongqiang:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
/usr/bin/tmux
/usr/bin/screen
(base) yongqiang@yongqiang:~$

Bash 的绝对路径:

(base) yongqiang@yongqiang:~$ which bash
/bin/bash
(base) yongqiang@yongqiang:~$

2. shebang

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.
shebang 是一个由井号和叹号构成的字符序列 #!,其出现在文本文件的第一行的前两个字符。

The shebang line is usually ignored by the interpreter, because the # character is a comment marker in many scripting languages; some language interpreters that do not use the hash mark to begin comments still may ignore the shebang line in recognition of its purpose.
由于 # 符号在许多脚本语言中都是注释标识符,shebang 的内容会被这些脚本解释器自动忽略。在 # 字符不是注释标识符的语言中,解释器也可能忽略以 #! 开头的首行内容,以提供与 shebang 的兼容性。

The name shebang for the distinctive two characters may have come from an inexact contraction of SHArp bang or haSH bang, referring to the two typical Unix names for them. Another theory on the sh in shebang is that it is from the default shell sh, usually invoked with shebang.

shebang 的名字来自于 SHArp bang or haSH bang 的缩写,指代 shebang#! 两个符号的典型 Unix 名称。在 Unix 术语中,井号通常称为 sharp, hash or mesh;而叹号则常常称为 bang。

contraction [k?n?tr?k?n]:n. 收缩,缩小,(肌肉的) 收缩,挛缩,子宫收缩,词的缩约形式
bang [b??]:v. 猛敲,砸,(把 ...) 砰的关上,猛摔,砰地一扔,碰撞,和 (女性) 性交 n. 突然的巨响,(对身体部位的) 猛撞,猛敲,猛击,叹号 adv. 正好,完全地 int. (表示枪声等巨响) 砰

2.1 Syntax

The form of a shebang interpreter directive is as follows:

#!interpreter [optional-arg]

in which interpreter is generally an absolute path to an executable program. The optional argument is a string representing a single argument. White space after #! is optional.
shebang#! 开头,即井号和叹号。在开头字符之后,可以有 0 个,1 个或多个空白字符,后接解释器的绝对路径,用于调用解释器。

Interpreter directives allow scripts and data files to be used as commands, hiding the details of their implementation from users and other programs, by removing the need to prefix scripts with their interpreter on the command line.
解释器指令允许脚本和数据文件充当系统命令,无需在调用时由用户指定解释器,从而对用户和其它程序隐藏其实现细节。

exclamation [.ekskl?'me??(?)n]:n. 感叹,感叹词,感叹语

#! is followed by /usr/bin/env, followed by the desired command without full path, as in this example:

#!/usr/bin/env sh

This mostly works because the path /usr/bin/env is commonly used for the env utility, and it invokes the first sh found in the user’s $PATH, typically /bin/sh.
使用 #!/usr/bin/env sh 脚本解释器名称是一种常见的在不同平台上都能正确找到解释器的办法。

2.2 Examples

Some typical shebang lines:

  • #!/bin/sh - Execute the file using the Bourne shell, or a compatible shell, assumed to be in the /bin directory
  • #!/bin/bash - Execute the file using the Bash shell
  • #!/usr/bin/pwsh - Execute the file using PowerShell
  • #!/usr/bin/env python3 - Execute with a Python interpreter, using the env program search path to find it
  • #!/bin/false - Do nothing, but return a non-zero exit status, indicating failure.

For example, if a script is named with the path path/to/script, and it starts with the following line, #!/bin/sh, then the program loader is instructed to run the program /bin/sh, passing path/to/script as the first argument.
#!/bin/sh 作为行开头,程序加载器运行程序 /bin/sh,传递 path/to/script 作为第一个参数。

3. yongqiang

  1. 工作区 /home/yongqiang/bash_work
(base) yongqiang@yongqiang:~$ mkdir bash_work
(base) yongqiang@yongqiang:~$ cd bash_work/
  1. touch 命令创建一个空文件命名为 bash_script.sh
(base) yongqiang@yongqiang:~/bash_work$ touch bash_script.sh
(base) yongqiang@yongqiang:~/bash_work$ ll
total 0
drwxr-xr-x 1 yongqiang yongqiang 512 Apr 23 22:18 ./
drwxr-xr-x 1 yongqiang yongqiang 512 Apr 23 22:16 ../
-rw-r--r-- 1 yongqiang yongqiang   0 Apr 23 22:18 bash_script.sh
(base) yongqiang@yongqiang:~/bash_work$
  1. #! 指定解释器的绝对路径 /bin/bash
(base) yongqiang@yongqiang:~/bash_work$ vim ./bash_script.sh
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ cat ./bash_script.sh
#!/bin/bash

echo "Yongqiang Cheng!"

(base) yongqiang@yongqiang:~/bash_work$
  1. 添加执行权限并执行
    阅读权限 (r):允许查看文件内容
    写入权限 (w):允许修改文件内容
    执行权限 (x):允许运行编程文件或脚本
(base) yongqiang@yongqiang:~/bash_work$ chmod a+x ./bash_script.sh
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ./bash_script.sh
Yongqiang Cheng!
(base) yongqiang@yongqiang:~/bash_work$

下面的执行方式不需要在第一行指定解释器信息

(base) yongqiang@yongqiang:~/bash_work$ /bin/bash bash_script.sh
Yongqiang Cheng!
(base) yongqiang@yongqiang:~/bash_work$

4. GNU Bourne-Again SHell - bash

GNU Bash
https://www.gnu.org/software/bash/

GNU Bash manual
https://www.gnu.org/software/bash/manual/

Bash Reference Manual
https://www.gnu.org/software/bash/manual/bash.html

GNU Manuals Online
https://www.gnu.org/manual/

The GNU Bourne-Again SHell
https://tiswww.case.edu/php/chet/bash/bashtop.html

Bash is the GNU Project’s shell - the Bourne Again SHell. This is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and the C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard.

General Commands Manual - BASH

(base) yongqiang@yongqiang:~$ info bash
...
(base) yongqiang@yongqiang:~$ man bash
...
(base) yongqiang@yongqiang:~$ bash --help
GNU bash, version 4.4.20(1)-release-(x86_64-pc-linux-gnu)
Usage:  bash [GNU long option] [option] ...
        bash [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --rcfile
        --restricted
        --verbose
        --version
Shell options:
        -ilrsD or -c command or -O shopt_option         (invocation only)
        -abefhkmnptuvxBCHP or -o option
Type `bash -c "help set"' for more information about shell options.
Type `bash -c help' for more information about shell builtin commands.
Use the `bashbug' command to report bugs.

bash home page: <http://www.gnu.org/software/bash>
General help using GNU software: <http://www.gnu.org/gethelp/>
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ ls -l /usr/share/doc/bash/
total 84
-rw-r--r-- 1 root root  7853 Sep  8  2016 COMPAT.gz
-rw-r--r-- 1 root root  2921 Feb 18  1999 INTRO.gz
-rw-r--r-- 1 root root 27983 Aug 23  2016 NEWS.gz
-rw-r--r-- 1 root root  3702 May 10  2016 POSIX.gz
-rw-r--r-- 1 root root  1693 Jun  7  2019 RBASH
-rw-r--r-- 1 root root  3839 Jun  7  2019 README
-rw-r--r-- 1 root root  1919 Jun  7  2019 README.Debian.gz
-rw-r--r-- 1 root root  1105 Jun  7  2019 README.abs-guide
-rw-r--r-- 1 root root  3021 Jun  7  2019 README.commands.gz
lrwxrwxrwx 1 root root    31 Apr  2  2018 README.md.bash_completion.gz -> ../bash-completion/README.md.gz
-rw-r--r-- 1 root root  1357 Jun  7  2019 changelog.Debian.gz
-rw-r--r-- 1 root root 10231 Jun  7  2019 copyright
-rw-r--r-- 1 root root   727 Jun  7  2019 inputrc.arrows
(base) yongqiang@yongqiang:~$

References

https://yongqiang.blog.csdn.net/
https://en.wikipedia.org/wiki/Bourne_shell
https://en.wikipedia.org/wiki/Bash_(Unix_shell)
https://en.wikipedia.org/wiki/Shebang_(Unix)
https://www.w3cschool.cn/bashshell/
https://wangdoc.com/bash/index.html

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-04-26 12:16:02  更:2022-04-26 12:19:57 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 19:25:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码