a.sh
path=xxx
经常使用source xxpath/a.sh 调用a.sh
在a.sh中如何获取自身所在文件夹路径呢?
1
eamars 2015-01-13 15:13:04 +08:00
cd "$(dirname "$0")"
|
2
gamecreating 2015-01-13 15:16:53 +08:00
pwd
|
4
eamars 2015-01-13 15:23:05 +08:00
|
5
bellchu 2015-01-13 15:26:09 +08:00
realpath
|
12
Beebird 2015-01-13 16:20:29 +08:00
#!/bin/env bash
... LOCATION=$(pwd) #echo $LOCATION ... |
14
lululau 2015-01-13 16:39:18 +08:00
|
18
Beebird 2015-01-13 16:52:38 +08:00
@otmb 试了可以啊,不论是source还是直接运行。
cat testpwd.sh #!/bin/env bash LOCATION=$(pwd) echo $LOCATION $ source testpwd.sh /home/lzhao/tmp |
19
HowardMei 2015-01-13 17:09:34 +08:00
sh和bash下面source和直接执行需要的判断参数不同,从"$_" "$0" "$BASH_SOURCE"三个里面找,如果是curl或cat script.sh | bash方式,
这三个都没用,参考: https://github.com/HowardMei/shbin/blob/master/shbin/shsrc |
20
KentY 2015-01-13 17:22:58 +08:00
|