script full name and path $0 not visible when called

懵懂的女人 提交于 2019-12-05 12:37:54

When you invoke it as bash task.sh, bash assigns "task.sh" to $0 (from the bash manual: "If Bash is invoked with a file of commands [...] $0 is set to the name of that file.").

When you source the file, bash does not alter $0, it just executes the script in the current environment. What's in $0 in your current enviroment?

$ echo "$0"
-bash

The leading dash will be interpreted by dirname as an option.

If it's in a cron job, why are you sourcing it?

If you need to source your script, this will work if your shell is bash:

SCRIPTPATH="${CUR_DIR}/${BASH_ARGV[0]}"

However, cron's shell is, I believe, /bin/sh. Even if /bin/sh is a symlink to bash, when bash is invoked as sh it will try to behave POSIXly: the BASH_ARGV array probably won't be available to you.

There is no reason to call external binaries such as pwd and dirname when using bash. The functionality of these two binaries can be replicated with pure shell syntax.

Try the following:

#!/bin/bash

CUR_DIR="$PWD"
SCRIPTPATH="${CUR_DIR}/${0#*/}"
Pedro Inácio

When you type,

bash foo.sh

you are executing script foo.sh, and bash sets the input argument $0 to the name of the script which is being run.

When you type,

. foo.sh

you are sourcing the script and the input argument $0 is not set. In this situation you can use the automatic variable $_ which contains the argument of the last executed command. In your script you could type,

SCRIPTPATH=$(dirname "$_")

to get the path of foo.sh. Notice that, for this to work, this has to be the first command executed in the file. Otherwise $_ will not contain the path of the sourced script.

Kudos to Dennis Williamson for providing this answer to a similar question.

I have used this for a long time without issues.

SCRIPTPATH=$(cd `dirname -- $0` && pwd)

The -- disable further processing of parameters.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!