我正在写一个bash脚本 . 我需要当前的工作目录始终是脚本所在的目录 .
默认行为是脚本中的当前工作目录是我运行它的shell的目录,但我不想要这种行为 .
#!/bin/bash cd "$(dirname "$0")"
以下也有效:
cd "${0%/*}"
语法在this StackOverflow答案中有详细描述 .
尝试以下简单的单行:
For all UNIX/OSX/Linux
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
注意:命令中使用双短划线( - )表示命令选项的结束,因此包含短划线或其他特殊字符的文件不会破坏命令 .
*For Linux, Mac and other BSD:
cd $(dirname $(realpath $0))
白色空间支持:
cd "$(dirname "$(realpath "$0")")";
注意: realpath 默认情况下应该安装在最流行的Linux发行版中(比如Ubuntu),但在某些情况下它可能会丢失,所以你必须安装它 .
realpath
否则你可以尝试类似的东西(它将使用第一个现有的工具):
cd $(dirname $(readlink -f $0 || realpath $0))
For Linux specific:
cd $(dirname $(readlink -f $0))
*Using GNU readlink on BSD/Mac:
cd $(dirname $(greadlink -f $0))
注意:您需要安装 coreutils (例如1.安装Homebrew,2 . brew install coreutils ) .
coreutils
brew install coreutils
In bash
在bash中你可以使用Parameter Expansions实现它,如:
cd ${0%/*}
但如果脚本从同一目录运行,则它不起作用 .
或者,您可以在bash中定义以下函数:
realpath () { [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}" }
此函数需要1个参数 . 如果参数已经是绝对路径,则按原样打印,否则打印$ PWD变量filename参数(不带./前缀) .
或者这是从Debian .bashrc 文件中获取的版本:
.bashrc
function realpath() { f=$@ if [ -d "$f" ]; then base="" dir="$f" else base="/$(basename "$f")" dir=$(dirname "$f") fi dir=$(cd "$dir" && /bin/pwd) echo "$dir$base" }
有关:
How to detect the current directory in which I run my shell script?
Getting the source directory of a Bash script from within
Bash script absolute path with OSX
Reliable way for a bash script to get the full path to itself?
也可以看看:
How can I get the behavior of GNU's readlink -f on a Mac?
cd "$(dirname ${BASH_SOURCE[0]})"
这很简单 . 有用 .
接受的答案适用于尚未在其他地方符号链接的脚本,例如 $PATH .
$PATH
但是,如果脚本是通过符号链接运行的,
ln -sv ~/project/script.sh ~/bin/; ~/bin/script.sh
这将进入 ~/bin/ 目录而不是 ~/project/ 目录,如果 cd 的目的是包含相对于 ~/project/ 的依赖项,这可能会破坏您的脚本
~/bin/
~/project/
cd
符号链接安全答案如下:
#!/bin/bash cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
需要 readlink -f 来解析潜在符号链接文件的绝对路径 .
readlink -f
引号是必需的,以支持可能包含空格的文件路径(不好的做法,但不能安全地假设这不是这种情况)
这个脚本似乎对我有用:
#!/bin/bash mypath=`realpath $0` cd `dirname $mypath` pwd
pwd命令行将脚本的位置作为当前工作目录回显,无论我从哪里运行它 .
获取脚本的真实路径
if [ -L $0 ] ; then ME=$(readlink $0) else ME=$0 fi DIR=$(dirname $ME)
(这是我在这里回答的问题:Get the name of the directory where a script is executed)
cd "`dirname $(readlink -f ${0})`"
echo $PWD
PWD是一个环境变量 .
如果您只需要打印当前工作目录,那么您可以按照此操作 .
$ vim test #!/bin/bash pwd :wq to save the test file.
授予执行权限:
chmod u+x test
然后通过 ./test 执行脚本,然后您可以看到当前的工作目录 .
./test
10 回答
以下也有效:
语法在this StackOverflow答案中有详细描述 .
尝试以下简单的单行:
For all UNIX/OSX/Linux
注意:命令中使用双短划线( - )表示命令选项的结束,因此包含短划线或其他特殊字符的文件不会破坏命令 .
*For Linux, Mac and other BSD:
白色空间支持:
注意:
realpath
默认情况下应该安装在最流行的Linux发行版中(比如Ubuntu),但在某些情况下它可能会丢失,所以你必须安装它 .否则你可以尝试类似的东西(它将使用第一个现有的工具):
For Linux specific:
*Using GNU readlink on BSD/Mac:
注意:您需要安装
coreutils
(例如1.安装Homebrew,2 .brew install coreutils
) .In bash
在bash中你可以使用Parameter Expansions实现它,如:
但如果脚本从同一目录运行,则它不起作用 .
或者,您可以在bash中定义以下函数:
此函数需要1个参数 . 如果参数已经是绝对路径,则按原样打印,否则打印$ PWD变量filename参数(不带./前缀) .
或者这是从Debian
.bashrc
文件中获取的版本:有关:
How to detect the current directory in which I run my shell script?
Getting the source directory of a Bash script from within
Bash script absolute path with OSX
Reliable way for a bash script to get the full path to itself?
也可以看看:
How can I get the behavior of GNU's readlink -f on a Mac?
这很简单 . 有用 .
接受的答案适用于尚未在其他地方符号链接的脚本,例如
$PATH
.但是,如果脚本是通过符号链接运行的,
这将进入
~/bin/
目录而不是~/project/
目录,如果cd
的目的是包含相对于~/project/
的依赖项,这可能会破坏您的脚本符号链接安全答案如下:
需要
readlink -f
来解析潜在符号链接文件的绝对路径 .引号是必需的,以支持可能包含空格的文件路径(不好的做法,但不能安全地假设这不是这种情况)
这个脚本似乎对我有用:
pwd命令行将脚本的位置作为当前工作目录回显,无论我从哪里运行它 .
获取脚本的真实路径
(这是我在这里回答的问题:Get the name of the directory where a script is executed)
PWD是一个环境变量 .
如果您只需要打印当前工作目录,那么您可以按照此操作 .
授予执行权限:
然后通过
./test
执行脚本,然后您可以看到当前的工作目录 .