Tra*_*v92 8 directory bash pwd
所以我有一个bash脚本调用另一个bash脚本.第二个脚本位于不同的文件夹中.
script1.sh:
"some_other_folder/script2.sh"
# do something
script2.sh:
src=$(pwd) # THIS returns current directory of script1.sh...
# do something
Run Code Online (Sandbox Code Playgroud)
在第二个脚本中它有行src=$(pwd)
,因为我从另一个目录中的另一个脚本调用该脚本,所以$(pwd)
返回第一个脚本的当前目录.
有没有办法在该脚本中使用简单命令获取第二个脚本的当前目录而不必传递参数?
谢谢.
我相信您正在寻找${BASH_SOURCE[0]}
, readlink
and dirname
(尽管您可以使用 bash 字符串替换来避免 dirname)
[jaypal:~/Temp] cat b.sh
#!/bin/bash
./tp/a.sh
[jaypal:~/Temp] pwd
/Volumes/Data/jaypalsingh/Temp
[jaypal:~/Temp] cat tp/a.sh
#!/bin/bash
src=$(pwd)
src2=$( dirname $( readlink -f ${BASH_SOURCE[0]} ) )
echo "$src"
echo "$src2"
[jaypal:~/Temp] ./b.sh
/Volumes/Data/jaypalsingh/Temp
/Volumes/Data/jaypalsingh/Temp/tp/
Run Code Online (Sandbox Code Playgroud)