linux中执行可执行文件的命令(./,.,source,sh,bash)

在Linux中,source、sh、bash和./都可以执行shell脚本,但它们有所不同。source在当前shell环境中执行,而sh和bash会在新的子shell中运行。./则是通过指定文件路径在子shell中执行脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

博客园连接:
在linux里,source、sh、bash、./都可以执行shell script文件,那它们有什么不同吗?
博客园连接

个人认为source、sh、bash,. 这几个都是将文件当sh文件来执行的,如果是py文件乃至其他文件那,那么他将不能识别(linux中需要在第一行申明文件执行的程序)
直接将文件绝对路径放到命令行或者./xxxxx放到命令行,也会执行,执行是按照你申明的程序来执行的

source 在当前shell内去读取
sh 都是打开一个subshell去读取(子shell)
bash 都是打开一个subshell去读取(子shell)
./ 打开一个subshell去读取




sh文件的执行
[root@iZbp11vz1brexya9wf6w6eZ home]# ll
total 8
drwx------ 2 weihang weihang 4096 Jul 16 21:17 weihang
drwxr-xr-x 2 root    root    4096 Jul  5 22:23 weihang2

[root@iZbp11vz1brexya9wf6w6eZ home]# cat /home/weihang/test.sh

#!/usr/bin/bash
echo "11111111111111111111111111111111111"

[root@iZbp11vz1brexya9wf6w6eZ home]# ./weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# ./weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# /weihang/test.sh
-bash: /weihang/test.sh: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# ./weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# source /weihang/test.sh
-bash: /weihang/test.sh: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# source /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# . /home/weihang/test.sh
11111111111111111111111111111111111


[root@iZbp11vz1brexya9wf6w6eZ home]# vim /home/weihang/test.sh
[root@iZbp11vz1brexya9wf6w6eZ home]# cat /home/weihang/test.sh

echo "11111111111111111111111111111111111"
[root@iZbp11vz1brexya9wf6w6eZ home]#
[root@iZbp11vz1brexya9wf6w6eZ home]# . /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# source /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# ./home/weihang/test.sh
-bash: ./home/weihang/test.sh: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# sh /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# bash /home/weihang/test.sh
11111111111111111111111111111111111
[root@iZbp11vz1brexya9wf6w6eZ home]# `/home/weihang/test.sh`
-bash: 11111111111111111111111111111111111: command not found
[root@iZbp11vz1brexya9wf6w6eZ home]#






py文件的执行

[root@iZbp11vz1brexya9wf6w6eZ home]# cat weihang/pytest.py
#!/usr/bin/python
print "sssssssssssssssssss"
a = [1,2,3,4,5]
print a
[root@iZbp11vz1brexya9wf6w6eZ home]#

-rwxrwxrwx 1 root root 70 Jul 16 21:44 pytest.py
-rwxrwxrwx 1 root root 44 Jul 16 21:33 test.sh
[root@iZbp11vz1brexya9wf6w6eZ weihang]# sh pytest.py
pytest.py: line 2: print: command not found
pytest.py: line 3: a: command not found
pytest.py: line 4: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# bash pytest.py
pytest.py: line 2: print: command not found
pytest.py: line 3: a: command not found
pytest.py: line 4: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# ./pytest.py
sssssssssssssssssss
[1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ weihang]# . pytest.py
-bash: print: command not found
-bash: a: command not found
-bash: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# . pytest.py
-bash: print: command not found
-bash: a: command not found
-bash: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# source pytest.py
-bash: print: command not found
-bash: a: command not found
-bash: print: command not found
[root@iZbp11vz1brexya9wf6w6eZ weihang]# python pytest.py
sssssssssssssssssss
[1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ weihang]# cd ..
[root@iZbp11vz1brexya9wf6w6eZ home]# ll
total 8
drwx------ 2 weihang weihang 4096 Jul 16 21:44 weihang
drwxr-xr-x 2 root    root    4096 Jul  5 22:23 weihang2
[root@iZbp11vz1brexya9wf6w6eZ home]# pwd
/home
[root@iZbp11vz1brexya9wf6w6eZ home]# /home/weihang/pytest.py
sssssssssssssssssss
[1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ home]# ./home/weihang/pytest.py
-bash: ./home/weihang/pytest.py: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# pwd
/home
[root@iZbp11vz1brexya9wf6w6eZ home]# /weihang/pytest.py
-bash: /weihang/pytest.py: No such file or directory
[root@iZbp11vz1brexya9wf6w6eZ home]# ./weihang/pytest.py
sssssssssssssssssss
[1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ home]#

[root@iZbp11vz1brexya9wf6w6eZ weihang]# a=`/home/weihang/pytest.py`
[root@iZbp11vz1brexya9wf6w6eZ weihang]# echo ${a}
sssssssssssssssssss [1, 2, 3, 4, 5]
[root@iZbp11vz1brexya9wf6w6eZ weihang]#





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值