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 2020-12-24 11:54  ZhangZhihuiAAA  阅读(73)  评论(0编辑  收藏  举报