方法有三种:

1 使用source

2 使用 .

3 使用sh

简单实验:

first.sh

#!/bin/bash
echo 'your are in first file'

 

second.sh

#!/bin/bash
echo 'your are in second file'

source first.sh // . first.sh // sh first.sh

 

执行结果:

your are in second file
your are in first file

 

现在讨论关于参数传递:

first.sh

#!/bin/bash
echo 'your are in first file'
echo "${0} ${1}"

second.sh

#!/bin/bash
echo 'your are in second file'
echo "${0} ${1}"

. first.sh ${2} //source first.sh

执行:./second.sh abc 123

your are in second file
./second.sh abc
your are in first file
./second.sh 123

 

改变second.sh

second.sh

#!/bin/bash
echo 'your are in second file'
echo "${0} ${1}"

sh first.sh ${2} 

执行:

./second.sh abc 123

结果:

your are in second file
./second.sh abc
your are in first file
first.sh 123

所以在调用的那个脚本需要传递参数的时候还是要注意选对方法的

 

相关文章:

  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2021-08-23
  • 2018-05-27
  • 2021-09-13
猜你喜欢
  • 2022-12-23
  • 2021-05-28
  • 2022-03-02
  • 2021-12-01
  • 2022-12-23
  • 2021-12-28
  • 2021-06-16
相关资源
相似解决方案