ZhangZhihui's Blog  

The shebang must be the first line, because it is interpreted by the kernel, which looks at the two bytes at the start of an executable file. If these are #! the rest of the line is interpreted as the executable to run and with the script file available to that program. (Details vary slightly, but that is the picture).

Since the kernel will only look at the first two characters and has no notion of further lines, you must place the hash bang in line 1.

 

If a shell script has a zsh shebang, when you run it in bash shell with dot slash:

复制代码
zzh@ZZHPC:~/aaa$ ./test_zsh_shebang.sh 
sleep 500s
^C
zzh@ZZHPC:~/aaa$ cat test_zsh_shebang.sh 
#!/bin/zsh

echo 'sleep 500s'
sleep 500
zzh@ZZHPC:~/aaa$ echo $SHELL
/bin/bash
zzh@ZZHPC:~/aaa$ ./test_zsh_shebang.sh 
sleep 500s
复制代码

You'll see it is run by zsh:

zzh@ZZHPC:~$ psvgrep test_zsh_shebang
zzh         6957    5189  0 10:44 pts/0    00:00:00 /bin/zsh ./test_zsh_shebang.sh

 

If a shell script has a bash shebang, when you run it in zsh shell with dot slash:

复制代码
zzh@ZZHPC:~/aaa$ zsh

2024-05-17 11:01AM
[zzh@(ZZHPC):/home/zzh/aaa]$>echo $SHELL
/bin/bash

2024-05-17 11:01AM
[zzh@(ZZHPC):/home/zzh/aaa]$>cat test_bash_shebang.sh 
#!/bin/bash

echo 'sleep 500s'
sleep 500

2024-05-17 11:02AM
[zzh@(ZZHPC):/home/zzh/aaa]$>./test_bash_shebang.sh 
sleep 500s
复制代码

You'll see it is run by bash:

zzh@ZZHPC:~$ psvgrep test_bash_shebang
zzh         6983    6972  0 10:47 pts/0    00:00:00 /bin/bash ./test_bash_shebang.sh

 

If a shell script doesn't have a shebang line, when you run it wit dot slash:

In bash shell (Ubuntu default shell):

zzh@ZZHPC:~/aaa$ cat test_no_shebang.sh 
echo 'sleep 500s'
sleep 500
zzh@ZZHPC:~/aaa$ ./test_no_shebang.sh 
sleep 500s

 

zzh@ZZHPC:~$ alias psvgrep
alias psvgrep='ps -ef | grep -v grep | grep'
zzh@ZZHPC:~$ psvgrep test_no_shebang
zzh@ZZHPC:~$ psvgrep sleep
zzh         6999    6998  0 10:51 pts/0    00:00:00 sleep 500
zzh@ZZHPC:~$ psvgrep 6998
zzh         6998    5189  0 10:51 pts/0    00:00:00 /bin/bash
zzh         6999    6998  0 10:51 pts/0    00:00:00 sleep 500

it is run by bash, and you CANNOT FIND THE PROCESS BY GREPPING THE SCRIPT NAME in the output of 'ps -ef'.

In zsh shell:

复制代码
zzh@ZZHPC:~/aaa$ zsh

2024-05-17 11:05AM
[zzh@(ZZHPC):/home/zzh/aaa]$>echo $SHELL
/bin/bash

2024-05-17 11:05AM
[zzh@(ZZHPC):/home/zzh/aaa]$>./test_no_shebang.sh 
sleep 500s
复制代码

 

zzh@ZZHPC:~$ psvgrep test_no_shebang
zzh         7172    7169  0 11:05 pts/0    00:00:00 sh ./test_no_shebang.sh
zzh@ZZHPC:~$ which sh
/usr/bin/sh
zzh@ZZHPC:~$ lh /usr/bin/sh
lrwxrwxrwx 1 root root 4 Jun 12  2023 /usr/bin/sh -> dash
zzh@ZZHPC:~$ lh /usr/bin/dash
-rwxr-xr-x 1 root root 123K Mar 23  2022 /usr/bin/dash

it is run by sh, which is a soft link pointing to /usr/bin/dash, and you can find the process by grepping the script name in the output of 'ps -ef'.

 

posted on   ZhangZhihuiAAA  阅读(78)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示