Unix shell脚本查找脚本文件所在的目录?

时间:2021-01-22 02:24:14

Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?

基本上,我需要使用与shell脚本文件位置相关的路径运行脚本,如何将当前目录更改为脚本文件所在的目录?

13 个解决方案

#1


413  

In Bash, you should get what you need like this:

在Bash中,您应该得到如下所需:

#!/usr/bin/env bash

BASEDIR=$(dirname "$0")
echo "$BASEDIR"

#2


315  

The original post contains the solution (ignore the responses, they don't add anything useful). The interesting work is done by the mentioned unix command readlink with option -f. Works when the script is called by an absolute as well as by a relative path.

原来的post包含了解决方案(忽略响应,它们不添加任何有用的内容)。有趣的工作是由上面提到的unix命令readlink和选项-f完成的。当脚本被一个绝对路径和一个相对路径调用时,它就会工作。

For bash, sh, ksh:

bash,sh凯驰(上海):

#!/bin/bash 
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=$(dirname "$SCRIPT")
echo $SCRIPTPATH

For tcsh, csh:

tcsh,csh:

#!/bin/tcsh
# Absolute path to this script, e.g. /home/user/bin/foo.csh
set SCRIPT=`readlink -f "$0"`
# Absolute path this script is in, thus /home/user/bin
set SCRIPTPATH=`dirname "$SCRIPT"`
echo $SCRIPTPATH

See also: https://*.com/a/246128/59087

参见:https://*.com/a/246128/59087

#3


37  

Assuming you're using bash

假设您正在使用bash

#!/bin/bash

current_dir=$(pwd)
script_dir=$(dirname $0)

echo $current_dir
echo $script_dir

This script should print the directory that you're in, and then the directory the script is in. For example, when calling it from / with the script in /home/mez/, it outputs

这个脚本应该打印您所在的目录,然后显示脚本所在的目录。例如,当使用/home/mez/中的脚本调用它时,它会输出

/
/home/mez

Remember, when assigning variables from the output of a command, wrap the command in $( and ) - or you won't get the desired output.

请记住,当从一个命令的输出中分配变量时,用$(和)来包装命令,否则就不会得到所需的输出。

#4


33  

An earlier comment on an answer said it, but it is easy to miss among all the other answers.

早些时候对一个答案的评论是这样说的,但是很容易忽略其他答案。

When using bash:

当使用bash:

echo this file: $BASH_SOURCE
echo this dir: `dirname $BASH_SOURCE`

Bash Reference Manual, 5.2 Bash Variables

Bash参考手册,5.2 Bash变量

#5


30  

If you're using bash....

如果你使用bash ....

#!/bin/bash

pushd $(dirname "${0}") > /dev/null
basedir=$(pwd -L)
# Use "pwd -P" for the path without links. man bash for more info.
popd > /dev/null

echo "${basedir}"

#6


14  

As theMarko suggests:

theMarko建议:

BASEDIR=$(dirname $0)
echo $BASEDIR

This works unless you execute the script from the same directory where the script resides, in which case you get a value of '.'

除非您从脚本所在的目录执行脚本,在这种情况下,您会得到'的值。

To get around that issue use:

为了避开这个问题,使用:

current_dir=$(pwd)
script_dir=$(dirname $0)

if [ $script_dir = '.' ]
then
script_dir="$current_dir"
fi

You can now use the variable current_dir throughout your script to refer to the script directory. However this may still have the symlink issue.

现在可以在整个脚本中使用变量current_dir来引用脚本目录。然而,这可能仍然存在着symlink问题。

#7


12  

cd $(dirname $(readlink -f $0))

#8


7  

The best answer for this question was answered here:
Getting the source directory of a Bash script from within

这个问题的最佳答案是:从内部获取Bash脚本的源目录。

And is:

和是:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

One-liner which will give you the full directory name of the script no matter where it is being called from.

一行代码,无论从哪里调用脚本,它都将为您提供脚本的完整目录名。

To understand how it works you can execute the following script:

要理解它是如何工作的,您可以执行以下脚本:

#!/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" )" && pwd )"
if [ "$DIR" != "$RDIR" ]; then
  echo "DIR '$RDIR' resolves to '$DIR'"
fi
echo "DIR is '$DIR'"

#9


4  

Let's make it a POSIX oneliner:

我们把它写成POSIX oneliner:

a="/$0"; a=${a%/*}; a=${a#/}; a=${a:-.}; BASEDIR=$(cd "$a"; pwd)

Tested on many Bourne-compatible shells including the BSD ones.

在许多与部署兼容的shell(包括BSD)上进行了测试。

As far as I know I am the author and I put it into public domain. For more info see: https://www.jasan.tk/posix/2017/05/11/posix_shell_dirname_replacement

据我所知,我是作者,我把它放到了公共领域。更多信息请参见:https://www.jasan.tk/posix/2017/05/11/posix_shell_dirname_replace

#10


2  

Inspired by blueyed’s answer

灵感来自于旅行包的答案

read < <(readlink -f $0 | xargs dirname)
cd $REPLY

#11


2  

So many answers, all plausible, each with pro's and con's & slightly differeing objectives (which should probably be stated for each). Here's another solution that meets a primary objective of both being clear and working across all systems, on all bash (no assumptions about bash versions, or readlink or pwd options), and reasonably does what you'd expect to happen (eg, resolving symlinks is an interesting problem, but isn't usually what you actually want), handle edge cases like spaces in paths, etc., ignores any errors and uses a sane default if there are any issues.

有这么多的答案,都是合理的,每个都有正反两方面的目标(可能每个都有)。这是另一个解决方案,满足的主要目标是明确和工作在所有系统中,在所有bash(没有假设bash版本,或指向或pwd选项),合理并会发生什么(例如,解决符号链接是一个有趣的问题,但通常不是你真正想要的是什么),处理边界情况像空间路径,等等,忽略任何错误和使用一个理智的违约是否有任何问题。

Each component is stored in a separate variable that you can use individually:

每个组件都存储在一个单独的变量中,您可以单独使用:

# script path, filename, directory
PROG_PATH=${BASH_SOURCE[0]}      # this script's name
PROG_NAME=${PROG_PATH##*/}       # basename of script (strip path)
PROG_DIR="$(cd "$(dirname "${PROG_PATH:-$PWD}")" 2>/dev/null 1>&2 && pwd)"

#12


0  

INTRODUCTION

介绍

This answer corrects the very broken but shockingly top voted answer of this thread (written by TheMarko):

这个答案纠正了这条线索(由TheMarko撰写)中非常破损但令人震惊的顶部投票的答案:

#!/usr/bin/env bash

BASEDIR=$(dirname "$0")
echo "$BASEDIR"

WHY DOES USING dirname "$0" ON IT'S OWN NOT WORK?

为什么单独使用dirname“$0”不能工作?

dirname $0 will only work if user launches script in a very specific way. I was able to find several situations where this answer fails and crashes the script.

只有当用户以非常特定的方式启动脚本时,dirname $0才能工作。我找到了几种情况,在这些情况下,这个答案会失败并导致脚本崩溃。

First of all, let's understand how this answer works. He's getting the script directory by doing

首先,让我们了解这个答案是如何工作的。他通过这样做获得了脚本目录

dirname "$0"

$0 represents the first part of the command calling the script (it's basically the inputted command without the arguments:

$0表示调用脚本的命令的第一部分(它基本上是没有参数的输入命令:

/some/path/./script argument1 argument2

$0="/some/path/./script"

$ 0 =“/一些/道路/ /脚本”。

dirname basically finds the last / in a string and truncates it there. So if you do:

dirname基本上会找到字符串中的最后一个/并在那里截断它。如果你做的事:

  dirname /usr/bin/sha256sum

you'll get: /usr/bin

你会得到:工作

This example works well because /usr/bin/sha256sum is a properly formatted path but

这个示例运行得很好,因为/usr/bin/sha256sum是一个格式正确的路径

  dirname "/some/path/./script"

wouldn't work well and would give you:

不会很好地工作,会给你:

  BASENAME="/some/path/." #which would crash your script if you try to use it as a path

Say you're in the same dir as your script and you launch it with this command

假设您与脚本位于同一个目录中,并使用此命令启动它

./script   

$0 in this situation will be ./script and dirname $0 will give:

在这种情况下,$0将是。/script和dirname $0将给出:

. #or BASEDIR=".", again this will crash your script

Using:

使用:

sh script

Without inputting the full path will also give a BASEDIR="."

不输入完整路径也会给出BASEDIR="。"

Using relative directories:

使用相对目录:

 ../some/path/./script

Gives a dirname $0 of:

给一个名字$0:

 ../some/path/.

If you're in the /some directory and you call the script in this manner (note the absence of / in the beginning, again a relative path):

如果您在/some目录中,并以这种方式调用脚本(请注意开头没有/,同样是相对路径):

 path/./script.sh

You'll get this value for dirname $0:

你会得到dirname $0的值:

 path/. 

and ./path/./script (another form of the relative path) gives:

和。/ /路径。/脚本(相对路径的另一种形式)给出:

 ./path/.

The only two situations where basedir $0 will work is if the user use sh or touch to launch a script because both will result in $0:

basedir $0只适用于以下两种情况:用户使用sh或touch启动脚本,因为两者都将导致$0:

 $0=/some/path/script

which will give you a path you can use with dirname.

这将给你一个你可以使用dirname的路径。

THE SOLUTION

解决方案

You'd have account for and detect every one of the above mentioned situations and apply a fix for it if it arises:

你应该考虑并检测上述的每一种情况,并在出现以下情况时对其进行修复:

#!/bin/bash
#this script will only work in bash, make sure it's installed on your system.

#set to false to not see all the echos
debug=true

if [ "$debug" = true ]; then echo "\$0=$0";fi


#The line below detect script's parent directory. $0 is the part of the launch command that doesn't contain the arguments
BASEDIR=$(dirname "$0") #3 situations will cause dirname $0 to fail: #situation1: user launches script while in script dir ( $0=./script)
                                                                     #situation2: different dir but ./ is used to launch script (ex. $0=/path_to/./script)
                                                                     #situation3: different dir but relative path used to launch script
if [ "$debug" = true ]; then echo 'BASEDIR=$(dirname "$0") gives: '"$BASEDIR";fi                                 

if [ "$BASEDIR" = "." ]; then BASEDIR="$(pwd)";fi # fix for situation1

_B2=${BASEDIR:$((${#BASEDIR}-2))}; B_=${BASEDIR::1}; B_2=${BASEDIR::2}; B_3=${BASEDIR::3} # <- bash only
if [ "$_B2" = "/." ]; then BASEDIR=${BASEDIR::$((${#BASEDIR}-1))};fi #fix for situation2 # <- bash only
if [ "$B_" != "/" ]; then  #fix for situation3 #<- bash only
        if [ "$B_2" = "./" ]; then
                #covers ./relative_path/(./)script
                if [ "$(pwd)" != "/" ]; then BASEDIR="$(pwd)/${BASEDIR:2}"; else BASEDIR="/${BASEDIR:2}";fi
        else
                #covers relative_path/(./)script and ../relative_path/(./)script, using ../relative_path fails if current path is a symbolic link
                if [ "$(pwd)" != "/" ]; then BASEDIR="$(pwd)/$BASEDIR"; else BASEDIR="/$BASEDIR";fi
        fi
fi

if [ "$debug" = true ]; then echo "fixed BASEDIR=$BASEDIR";fi

#13


-3  

That should do the trick:

这应该能达到目的:

echo `pwd`/`dirname $0`

It might look ugly depending on how it was invoked and the cwd but should get you where you need to go (or you can tweak the string if you care how it looks).

根据调用的方式和cwd,它可能看起来很难看,但是应该让您找到需要的位置(或者,如果您关心字符串的外观,您可以对其进行调整)。

#1


413  

In Bash, you should get what you need like this:

在Bash中,您应该得到如下所需:

#!/usr/bin/env bash

BASEDIR=$(dirname "$0")
echo "$BASEDIR"

#2


315  

The original post contains the solution (ignore the responses, they don't add anything useful). The interesting work is done by the mentioned unix command readlink with option -f. Works when the script is called by an absolute as well as by a relative path.

原来的post包含了解决方案(忽略响应,它们不添加任何有用的内容)。有趣的工作是由上面提到的unix命令readlink和选项-f完成的。当脚本被一个绝对路径和一个相对路径调用时,它就会工作。

For bash, sh, ksh:

bash,sh凯驰(上海):

#!/bin/bash 
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=$(dirname "$SCRIPT")
echo $SCRIPTPATH

For tcsh, csh:

tcsh,csh:

#!/bin/tcsh
# Absolute path to this script, e.g. /home/user/bin/foo.csh
set SCRIPT=`readlink -f "$0"`
# Absolute path this script is in, thus /home/user/bin
set SCRIPTPATH=`dirname "$SCRIPT"`
echo $SCRIPTPATH

See also: https://*.com/a/246128/59087

参见:https://*.com/a/246128/59087

#3


37  

Assuming you're using bash

假设您正在使用bash

#!/bin/bash

current_dir=$(pwd)
script_dir=$(dirname $0)

echo $current_dir
echo $script_dir

This script should print the directory that you're in, and then the directory the script is in. For example, when calling it from / with the script in /home/mez/, it outputs

这个脚本应该打印您所在的目录,然后显示脚本所在的目录。例如,当使用/home/mez/中的脚本调用它时,它会输出

/
/home/mez

Remember, when assigning variables from the output of a command, wrap the command in $( and ) - or you won't get the desired output.

请记住,当从一个命令的输出中分配变量时,用$(和)来包装命令,否则就不会得到所需的输出。

#4


33  

An earlier comment on an answer said it, but it is easy to miss among all the other answers.

早些时候对一个答案的评论是这样说的,但是很容易忽略其他答案。

When using bash:

当使用bash:

echo this file: $BASH_SOURCE
echo this dir: `dirname $BASH_SOURCE`

Bash Reference Manual, 5.2 Bash Variables

Bash参考手册,5.2 Bash变量

#5


30  

If you're using bash....

如果你使用bash ....

#!/bin/bash

pushd $(dirname "${0}") > /dev/null
basedir=$(pwd -L)
# Use "pwd -P" for the path without links. man bash for more info.
popd > /dev/null

echo "${basedir}"

#6


14  

As theMarko suggests:

theMarko建议:

BASEDIR=$(dirname $0)
echo $BASEDIR

This works unless you execute the script from the same directory where the script resides, in which case you get a value of '.'

除非您从脚本所在的目录执行脚本,在这种情况下,您会得到'的值。

To get around that issue use:

为了避开这个问题,使用:

current_dir=$(pwd)
script_dir=$(dirname $0)

if [ $script_dir = '.' ]
then
script_dir="$current_dir"
fi

You can now use the variable current_dir throughout your script to refer to the script directory. However this may still have the symlink issue.

现在可以在整个脚本中使用变量current_dir来引用脚本目录。然而,这可能仍然存在着symlink问题。

#7


12  

cd $(dirname $(readlink -f $0))

#8


7  

The best answer for this question was answered here:
Getting the source directory of a Bash script from within

这个问题的最佳答案是:从内部获取Bash脚本的源目录。

And is:

和是:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

One-liner which will give you the full directory name of the script no matter where it is being called from.

一行代码,无论从哪里调用脚本,它都将为您提供脚本的完整目录名。

To understand how it works you can execute the following script:

要理解它是如何工作的,您可以执行以下脚本:

#!/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" )" && pwd )"
if [ "$DIR" != "$RDIR" ]; then
  echo "DIR '$RDIR' resolves to '$DIR'"
fi
echo "DIR is '$DIR'"

#9


4  

Let's make it a POSIX oneliner:

我们把它写成POSIX oneliner:

a="/$0"; a=${a%/*}; a=${a#/}; a=${a:-.}; BASEDIR=$(cd "$a"; pwd)

Tested on many Bourne-compatible shells including the BSD ones.

在许多与部署兼容的shell(包括BSD)上进行了测试。

As far as I know I am the author and I put it into public domain. For more info see: https://www.jasan.tk/posix/2017/05/11/posix_shell_dirname_replacement

据我所知,我是作者,我把它放到了公共领域。更多信息请参见:https://www.jasan.tk/posix/2017/05/11/posix_shell_dirname_replace

#10


2  

Inspired by blueyed’s answer

灵感来自于旅行包的答案

read < <(readlink -f $0 | xargs dirname)
cd $REPLY

#11


2  

So many answers, all plausible, each with pro's and con's & slightly differeing objectives (which should probably be stated for each). Here's another solution that meets a primary objective of both being clear and working across all systems, on all bash (no assumptions about bash versions, or readlink or pwd options), and reasonably does what you'd expect to happen (eg, resolving symlinks is an interesting problem, but isn't usually what you actually want), handle edge cases like spaces in paths, etc., ignores any errors and uses a sane default if there are any issues.

有这么多的答案,都是合理的,每个都有正反两方面的目标(可能每个都有)。这是另一个解决方案,满足的主要目标是明确和工作在所有系统中,在所有bash(没有假设bash版本,或指向或pwd选项),合理并会发生什么(例如,解决符号链接是一个有趣的问题,但通常不是你真正想要的是什么),处理边界情况像空间路径,等等,忽略任何错误和使用一个理智的违约是否有任何问题。

Each component is stored in a separate variable that you can use individually:

每个组件都存储在一个单独的变量中,您可以单独使用:

# script path, filename, directory
PROG_PATH=${BASH_SOURCE[0]}      # this script's name
PROG_NAME=${PROG_PATH##*/}       # basename of script (strip path)
PROG_DIR="$(cd "$(dirname "${PROG_PATH:-$PWD}")" 2>/dev/null 1>&2 && pwd)"

#12


0  

INTRODUCTION

介绍

This answer corrects the very broken but shockingly top voted answer of this thread (written by TheMarko):

这个答案纠正了这条线索(由TheMarko撰写)中非常破损但令人震惊的顶部投票的答案:

#!/usr/bin/env bash

BASEDIR=$(dirname "$0")
echo "$BASEDIR"

WHY DOES USING dirname "$0" ON IT'S OWN NOT WORK?

为什么单独使用dirname“$0”不能工作?

dirname $0 will only work if user launches script in a very specific way. I was able to find several situations where this answer fails and crashes the script.

只有当用户以非常特定的方式启动脚本时,dirname $0才能工作。我找到了几种情况,在这些情况下,这个答案会失败并导致脚本崩溃。

First of all, let's understand how this answer works. He's getting the script directory by doing

首先,让我们了解这个答案是如何工作的。他通过这样做获得了脚本目录

dirname "$0"

$0 represents the first part of the command calling the script (it's basically the inputted command without the arguments:

$0表示调用脚本的命令的第一部分(它基本上是没有参数的输入命令:

/some/path/./script argument1 argument2

$0="/some/path/./script"

$ 0 =“/一些/道路/ /脚本”。

dirname basically finds the last / in a string and truncates it there. So if you do:

dirname基本上会找到字符串中的最后一个/并在那里截断它。如果你做的事:

  dirname /usr/bin/sha256sum

you'll get: /usr/bin

你会得到:工作

This example works well because /usr/bin/sha256sum is a properly formatted path but

这个示例运行得很好,因为/usr/bin/sha256sum是一个格式正确的路径

  dirname "/some/path/./script"

wouldn't work well and would give you:

不会很好地工作,会给你:

  BASENAME="/some/path/." #which would crash your script if you try to use it as a path

Say you're in the same dir as your script and you launch it with this command

假设您与脚本位于同一个目录中,并使用此命令启动它

./script   

$0 in this situation will be ./script and dirname $0 will give:

在这种情况下,$0将是。/script和dirname $0将给出:

. #or BASEDIR=".", again this will crash your script

Using:

使用:

sh script

Without inputting the full path will also give a BASEDIR="."

不输入完整路径也会给出BASEDIR="。"

Using relative directories:

使用相对目录:

 ../some/path/./script

Gives a dirname $0 of:

给一个名字$0:

 ../some/path/.

If you're in the /some directory and you call the script in this manner (note the absence of / in the beginning, again a relative path):

如果您在/some目录中,并以这种方式调用脚本(请注意开头没有/,同样是相对路径):

 path/./script.sh

You'll get this value for dirname $0:

你会得到dirname $0的值:

 path/. 

and ./path/./script (another form of the relative path) gives:

和。/ /路径。/脚本(相对路径的另一种形式)给出:

 ./path/.

The only two situations where basedir $0 will work is if the user use sh or touch to launch a script because both will result in $0:

basedir $0只适用于以下两种情况:用户使用sh或touch启动脚本,因为两者都将导致$0:

 $0=/some/path/script

which will give you a path you can use with dirname.

这将给你一个你可以使用dirname的路径。

THE SOLUTION

解决方案

You'd have account for and detect every one of the above mentioned situations and apply a fix for it if it arises:

你应该考虑并检测上述的每一种情况,并在出现以下情况时对其进行修复:

#!/bin/bash
#this script will only work in bash, make sure it's installed on your system.

#set to false to not see all the echos
debug=true

if [ "$debug" = true ]; then echo "\$0=$0";fi


#The line below detect script's parent directory. $0 is the part of the launch command that doesn't contain the arguments
BASEDIR=$(dirname "$0") #3 situations will cause dirname $0 to fail: #situation1: user launches script while in script dir ( $0=./script)
                                                                     #situation2: different dir but ./ is used to launch script (ex. $0=/path_to/./script)
                                                                     #situation3: different dir but relative path used to launch script
if [ "$debug" = true ]; then echo 'BASEDIR=$(dirname "$0") gives: '"$BASEDIR";fi                                 

if [ "$BASEDIR" = "." ]; then BASEDIR="$(pwd)";fi # fix for situation1

_B2=${BASEDIR:$((${#BASEDIR}-2))}; B_=${BASEDIR::1}; B_2=${BASEDIR::2}; B_3=${BASEDIR::3} # <- bash only
if [ "$_B2" = "/." ]; then BASEDIR=${BASEDIR::$((${#BASEDIR}-1))};fi #fix for situation2 # <- bash only
if [ "$B_" != "/" ]; then  #fix for situation3 #<- bash only
        if [ "$B_2" = "./" ]; then
                #covers ./relative_path/(./)script
                if [ "$(pwd)" != "/" ]; then BASEDIR="$(pwd)/${BASEDIR:2}"; else BASEDIR="/${BASEDIR:2}";fi
        else
                #covers relative_path/(./)script and ../relative_path/(./)script, using ../relative_path fails if current path is a symbolic link
                if [ "$(pwd)" != "/" ]; then BASEDIR="$(pwd)/$BASEDIR"; else BASEDIR="/$BASEDIR";fi
        fi
fi

if [ "$debug" = true ]; then echo "fixed BASEDIR=$BASEDIR";fi

#13


-3  

That should do the trick:

这应该能达到目的:

echo `pwd`/`dirname $0`

It might look ugly depending on how it was invoked and the cwd but should get you where you need to go (or you can tweak the string if you care how it looks).

根据调用的方式和cwd,它可能看起来很难看,但是应该让您找到需要的位置(或者,如果您关心字符串的外观,您可以对其进行调整)。