标签云

微信群

扫码加入我们

WeChat QR Code

Bash被另一个bash脚本[复制]后得到当前目录的文件

This question already has an answer here:

So I have one bash script which calls another bash script. The second script is in a different folder.

script1.sh:
"some_other_folder/script2.sh"
# do something

script2.sh:
src=$(pwd) # THIS returns current directory of script1.sh...
# do something

In this second script it has the line src=$(pwd) and since I'm calling that script from another script in a different directory, the $(pwd) returns the current directory of the first script.

Is there any way to get the current directory of the second script using a simple command within that script without having to pass a parameter?

Thanks.


This is a SO FAQ: Can a Bash script tell what directory it's stored in?

2018年06月19日55分23秒

One comment on terminology. Current working directory refers to the single runtime value for each process - the directory in which it is running (i.e. answering the question, where is "."). A better way to ask the question is, "how do I locate the directory from which the second script is being executed".

2018年06月18日55分23秒

See also stackoverflow.com/questions/59895/….

2018年06月18日55分23秒

Thanks, this worked !

2018年06月19日55分23秒