如何从脚本本身中获取 Bash 脚本的源目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/59895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the source directory of a Bash script from within the script itself
提问by Jiaaro
How do I get the path of the directory in which a Bashscript is located, insidethat script?
如何获取其中的目录路径的Bash脚本位于,里面那个脚本?
I want to use a Bash script as a launcher for another application. I want to change the working directory to the one where the Bash script is located, so I can operate on the files in that directory, like so:
我想使用 Bash 脚本作为另一个应用程序的启动器。我想将工作目录更改为 Bash 脚本所在的目录,以便我可以对该目录中的文件进行操作,如下所示:
$ ./application
回答by dogbane
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
is a useful one-liner which will give you the full directory name of the script no matter where it is being called from.
是一个有用的单行程序,无论从何处调用脚本,它都会为您提供脚本的完整目录名称。
It will work as long as the last component of the path used to find the script is not a symlink (directory links are OK). If you also want to resolve any links to the script itself, you need a multi-line solution:
只要用于查找脚本的路径的最后一个组件不是符号链接(目录链接正常),它就会起作用。如果您还想解析脚本本身的任何链接,则需要多行解决方案:
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
This last one will work with any combination of aliases, source
, bash -c
, symlinks, etc.
最后一个将适用于别名、source
、bash -c
、 符号链接等的任意组合。
Beware: if you cd
to a different directory before running this snippet, the result may be incorrect!
注意:如果您cd
在运行此代码段之前到不同的目录,结果可能不正确!
Also, watch out for $CDPATH
gotchas, and stderr output side effects if the user has smartly overridden cd to redirect output to stderr instead (including escape sequences, such as when calling update_terminal_cwd >&2
on Mac). Adding >/dev/null 2>&1
at the end of your cd
command will take care of both possibilities.
此外,如果用户巧妙地覆盖 cd 以将输出重定向到 stderr(包括转义序列,例如在 Mac 上调用时),请注意$CDPATH
gotchas和 stderr 输出副作用update_terminal_cwd >&2
。>/dev/null 2>&1
在cd
命令末尾添加将处理这两种可能性。
To understand how it works, try running this more verbose form:
要了解它是如何工作的,请尝试运行这个更详细的表单:
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
TARGET="$(readlink "$SOURCE")"
if [[ $TARGET == /* ]]; then
echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"
SOURCE="$TARGET"
else
DIR="$( dirname "$SOURCE" )"
echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"
SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
fi
done
echo "SOURCE is '$SOURCE'"
RDIR="$( dirname "$SOURCE" )"
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
if [ "$DIR" != "$RDIR" ]; then
echo "DIR '$RDIR' resolves to '$DIR'"
fi
echo "DIR is '$DIR'"
And it will print something like:
它会打印如下内容:
SOURCE './scriptdir.sh' is a relative symlink to 'sym2/scriptdir.sh' (relative to '.')
SOURCE is './sym2/scriptdir.sh'
DIR './sym2' resolves to '/home/ubuntu/dotfiles/fo fo/real/real1/real2'
DIR is '/home/ubuntu/dotfiles/fo fo/real/real1/real2'
回答by matt b
Use dirname "$0"
:
使用dirname "$0"
:
#!/bin/bash
echo "The script you are running has basename `basename "[matt@server1 ~]$ pwd
/home/matt
[matt@server1 ~]$ ./test2.sh
The script you are running has basename test2.sh, dirname .
The present working directory is /home/matt
[matt@server1 ~]$ cd /tmp
[matt@server1 tmp]$ ~/test2.sh
The script you are running has basename test2.sh, dirname /home/matt
The present working directory is /tmp
"`, dirname `dirname "dirname "dirname "$(readlink -f "#!/bin/bash
echo "pwd: `pwd`"
echo "$0: >>>$ ./whatdir.sh
pwd: /Users/phatblat
>>>$ /Users/phatblat/whatdir.sh
pwd: /Users/phatblat
>>>$ cd /tmp
>>>$ ~/whatdir.sh
pwd: /tmp
>>>$ ln -s ~/whatdir.sh whatdirlink.sh
>>>$ ./whatdirlink.sh
pwd: /tmp
pushd . > /dev/null
SCRIPT_PATH="${BASH_SOURCE[0]}"
if ([ -h "${SCRIPT_PATH}" ]); then
while([ -h "${SCRIPT_PATH}" ]); do cd `dirname "$SCRIPT_PATH"`;
SCRIPT_PATH=`readlink "${SCRIPT_PATH}"`; done
fi
cd `dirname ${SCRIPT_PATH}` > /dev/null
SCRIPT_PATH=`pwd`;
popd > /dev/null
: ./whatdirlink.sh
basename: whatdirlink.sh
dirname: .
dirname/readlink: /Users/phatblat
: /Users/phatblat/whatdir.sh
basename: whatdir.sh
dirname: /Users/phatblat
dirname/readlink: /Users/phatblat
: /Users/phatblat/whatdir.sh
basename: whatdir.sh
dirname: /Users/phatblat
dirname/readlink: /Users/phatblat
: ./whatdir.sh
basename: whatdir.sh
dirname: .
dirname/readlink: /Users/phatblat
"
echo "basename: `basename pushd . > /dev/null
SCRIPT_PATH="${BASH_SOURCE[0]}";
if ([ -h "${SCRIPT_PATH}" ]) then
while([ -h "${SCRIPT_PATH}" ]) do cd `dirname "$SCRIPT_PATH"`; SCRIPT_PATH=`readlink "${SCRIPT_PATH}"`; done
fi
cd `dirname ${SCRIPT_PATH}` > /dev/null
SCRIPT_PATH=`pwd`;
popd > /dev/null
`"
echo "dirname: `dirname `dirname $(dirname "#!/bin/bash
scriptdir=`dirname "$BASH_SOURCE"`
")
`
`"
echo "dirname/readlink: $(dirname $(readlink -f DIR="$(dirname "$(readlink -f "#!/bin/bash
reldir=`dirname cd `dirname DIR="$(dirname "${BASH_SOURCE[0]}")" # get the directory name
DIR="$(realpath "${DIR}")" # resolve its full path if need be
`
`
cd $reldir
directory=`pwd`
echo "Directory is $directory"
")")"
))"
")"
"
"`"
echo "The present working directory is `pwd`"
using pwd
alone will not work if you are not running the script from the directory it is contained in.
pwd
如果您不是从脚本所在的目录运行脚本,则单独使用将不起作用。
./script
/usr/bin/script
script
回答by phatblat
The dirname
command is the most basic, simply parsing the path up to the filename off of the $0
(script name) variable:
该dirname
命令是最基本的,只需解析$0
(脚本名称)变量的文件名的路径:
But, as matt bpointed out, the path returned is different depending on how the script is called. pwd
doesn't do the job because that only tells you what the current directory is, not what directory the script resides in. Additionally, if a symbolic link to a script is executed, you're going to get a (probably relative) path to where the link resides, not the actual script.
但是,正如matt b 所指出的,根据脚本的调用方式,返回的路径是不同的。pwd
不做这项工作,因为它只告诉你当前目录是什么,而不是脚本所在的目录。此外,如果执行到脚本的符号链接,你将获得一个(可能是相对的)路径链接所在的位置,而不是实际的脚本。
Some others have mentioned the readlink
command, but at its simplest, you can use:
其他一些人提到了该readlink
命令,但最简单的是,您可以使用:
readlink
will resolve the script path to an absolute path from the root of the filesystem. So, any paths containing single or double dots, tildes and/or symbolic links will be resolved to a full path.
readlink
将脚本路径解析为文件系统根目录的绝对路径。因此,任何包含单点或双点、波浪线和/或符号链接的路径都将被解析为完整路径。
Here's a script demonstrating each of these, whatdir.sh
:
这是一个演示其中每一个的脚本,whatdir.sh
:
Running this script in my home dir, using a relative path:
使用相对路径在我的主目录中运行此脚本:
##代码##Again, but using the full path to the script:
同样,但使用脚本的完整路径:
##代码##Now changing directories:
现在更改目录:
##代码##And finally using a symbolic link to execute the script:
最后使用符号链接来执行脚本:
##代码##回答by user25866
Works for all versions,including
适用于所有版本,包括
- when called via multple depth soft link,
- when the file it
- when script called by command "
source
" aka.
(dot) operator. - when arg
$0
is modified from caller. "./script"
"/full/path/to/script"
"/some/path/../../another/path/script"
"./some/folder/script"
- 当通过多深度软链接调用时,
- 当文件它
- 当脚本被命令“
source
”又名.
(点)操作符调用时。 - 当
$0
调用者修改arg 时。 "./script"
"/full/path/to/script"
"/some/path/../../another/path/script"
"./some/folder/script"
Alternatively, if the bash script itself is a relative symlinkyou wantto follow it and return the full path of the linked-to script:
或者,如果 bash 脚本本身是一个相对符号链接,您希望遵循它并返回链接到脚本的完整路径:
##代码##SCRIPT_PATH
is given in full path, no matter how it is called.
Just make sure you locate this at start of the script.
SCRIPT_PATH
以完整路径给出,无论如何调用。
只要确保您在脚本的开头找到它。
This comment and code Copyleft, selectable license under the GPL2.0 or later or CC-SA 3.0 (CreativeCommons Share Alike) or later. (c) 2008. All rights reserved. No warranty of any kind. You have been warned.
http://www.gnu.org/licenses/gpl-2.0.txt
http://creativecommons.org/licenses/by-sa/3.0/
18eedfe1c99df68dc94d4a94712a71aaa8e1e9e36cacf421b9463dd2bbaa02906d0d6656
此评论和代码 Copyleft,GPL2.0 或更高版本或 CC-SA 3.0(CreativeCommons Share Alike)或更高版本下的可选许可证。(c) 2008。保留所有权利。没有任何形式的保证。你被警告了。
http://www.gnu.org/licenses/gpl-2.0.txt
http://creativecommons.org/licenses/by-sa/3.0/
18eedfe1c99df68dc94d4a94712a71aaa8e1e9e36cacf421b9463dd2bbaa02906d0d6656
回答by Fabien
回答by Mr Shark
You can use $BASH_SOURCE
:
您可以使用$BASH_SOURCE
:
Note that you need to use #!/bin/bash
and not #!/bin/sh
since it's a Bash extension.
请注意,您需要使用#!/bin/bash
and not#!/bin/sh
因为它是 Bash 扩展。
回答by Simon Rigét
This should do it:
这应该这样做:
##代码##This works with symlinks and spaces in path.
这适用于路径中的符号链接和空格。
See the man pages for dirname
and readlink
.
请参阅手册页dirname
和readlink
。
From the comment track it seems not to work with Mac OS. I have no idea why that is. Any suggestions?
从评论轨道它似乎不适用于 Mac OS。我不知道为什么会这样。有什么建议?
回答by SpoonMeiser
pwd
can be used to find the current working directory, and dirname
to find the directory of a particular file (command that was run, is $0
, so dirname $0
should give you the directory of the current script).
pwd
可用于查找当前工作目录,并dirname
查找特定文件的目录(运行的命令 is $0
,因此dirname $0
应该为您提供当前脚本的目录)。
However, dirname
gives precisely the directory portion of the filename, which more likely than not is going to be relative to the current working directory. If your script needs to change directory for some reason, then the output from dirname
becomes meaningless.
但是,dirname
精确给出文件名的目录部分,这很可能与当前工作目录相关。如果您的脚本由于某种原因需要更改目录,那么 from 的输出dirname
将变得毫无意义。
I suggest the following:
我建议如下:
##代码##This way, you get an absolute, rather then relative directory.
这样,您将获得一个绝对目录,而不是相对目录。
Since the script will be run in a separate bash instance, there is no need to restore the working directory afterwards, but if you do want to change back in your script for some reason, you can easily assign the value of pwd
to a variable before you change directory, for future use.
由于脚本将在单独的 bash 实例中运行,因此之后无需恢复工作目录,但如果您出于某种原因确实想改回脚本,您可以轻松地将 的值分配给pwd
变量之前更改目录,以备将来使用。
Although just
虽然只是
##代码##solves the specific scenario in the question, I find having the absolute path to more more useful generally.
解决了问题中的特定场景,我发现拥有更有用的绝对路径。
回答by Thamme Gowda
I am tired of coming to this page over and over to copy paste the one-liner in the accepted answer. The problem with that is it is not easy to understand and remember.
我厌倦了一遍又一遍地访问此页面以将单行复制粘贴到已接受的答案中。问题在于它不容易理解和记住。
Here is an easy-to-remember script:
这是一个易于记忆的脚本:
##代码##回答by Thamme Gowda
I don't think this is as easy as others have made it out to be. pwd
doesn't work, as the current directory is not necessarily the directory with the script. $0
doesn't always have the information either. Consider the following three ways to invoke a script:
我认为这并不像其他人所做的那样容易。 pwd
不起作用,因为当前目录不一定是包含脚本的目录。 $0
也不总是有信息。考虑以下三种调用脚本的方法:
In the first and third ways $0
doesn't have the full path information. In the second and third, pwd
does not work. The only way to get the directory in the third way would be to run through the path and find the file with the correct match. Basically the code would have to redo what the OS does.
第一种和第三种方式$0
没有完整的路径信息。在第二和第三,pwd
不起作用。以第三种方式获取目录的唯一方法是遍历路径并找到具有正确匹配项的文件。基本上,代码必须重做操作系统所做的事情。
One way to do what you are asking would be to just hardcode the data in the /usr/share
directory, and reference it by its full path. Data shoudn't be in the /usr/bin
directory anyway, so this is probably the thing to do.
执行您所要求的一种方法是仅对/usr/share
目录中的数据进行硬编码,并通过其完整路径引用它。/usr/bin
无论如何,数据不应该在目录中,所以这可能是要做的事情。