linux 中shell脚本的执行方法对路径的影响

 

4种;

01、bash a.sh

02、sh a.sh

03、. a.sh

04、source

 

前两种会重开shell终端;

后两种是本shell;

测试如下:

001、测试前两种方法;不影响当前路径

[root@pc1 test1]# ls
a.sh
[root@pc1 test1]# pwd             ## 当前路径
/home/test1
[root@pc1 test1]# cat a.sh        ## 脚本内容
#!/bin/bash

cd /home/
ls
[root@pc1 test1]# bash a.sh
liujiaxin01  outcome.map  software  SRR11076280  test1
[root@pc1 test1]# pwd            ## 执行1后路径
/home/test1
[root@pc1 test1]# sh a.sh
liujiaxin01  outcome.map  software  SRR11076280  test1
[root@pc1 test1]# pwd            ## 执行2后路径    
/home/test1

 

002、测试后两种;影响当前路径

[root@pc1 test1]# ls
a.sh
[root@pc1 test1]# cat a.sh
#!/bin/bash

cd /home/
ls
[root@pc1 test1]# pwd          ## 当前路径
/home/test1
[root@pc1 test1]# . a.sh
liujiaxin01  outcome.map  software  SRR11076280  test1
[root@pc1 home]# pwd           ## 执行后路径
/home
[root@pc1 home]# cd test1/
[root@pc1 test1]# ls
a.sh
[root@pc1 test1]# pwd           ##当前路径
/home/test1
[root@pc1 test1]# source a.sh
liujiaxin01  outcome.map  software  SRR11076280  test1
[root@pc1 home]# pwd           ## 执行后路径
/home

 。

 

posted @ 2024-02-20 15:14  小鲨鱼2018  阅读(12)  评论(0编辑  收藏  举报