Bash script 取得自己的檔案名稱

Bash 的 Shell script 要抓自己的檔案名稱,只要用 $0 就可以抓到,不過 Shell script 若被呼叫時,用 $0 抓到的名稱是如何呢?

另外,Bash 有哪些預設的變數可以使用呢?

Bash script 取得自己的檔案名稱

  • a.sh
    #!/bin/bash
    echo $0
    ./b.sh
  • b.sh
    #!/bin/bash
    echo $0
  • chmod +x ./a.sh ./b.sh

執行 ./a.sh,出現下述:

./a.sh
./b.sh

這個結果是正確的,都是我們要的執行檔檔名

但是需要環境變數來執行程式的情況,使用 source 來執行,程式改成下述:

  • a.sh
    #!/bin/bash
    echo $0
    source ./b.sh
  • b.sh
    #!/bin/bash
    echo $0
  • chmod +x ./a.sh ./b.sh

執行 ./a.sh,出現下述:

./a.sh
./a.sh

會出現都是 a.sh 來執行的,不會出現 b.sh

要解決此問題,只要將 b.sh 的 $0 改成 $BASH_SOURCE 即可,如下述:

  • ./b.sh
    #!/bin/bash
    echo $BASH_SOURCE

這樣子就可以達到想要的效果 (同 $0 的效果)

關於 $BASH_SOURCE 的說明文件

$BASH_SOURCE gives the correct answer when sourcing the script.
This however includes the path so to get the scripts filename only, use:
$(basename $BASH_SOURCE)

BASH_SOURCE

An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}.

FUNCNAME

An array variable containing the names of all shell
functions currently in the execution call stack. The
element with index 0 is the name of any
currently-executing shell function. The bottom-most
element (the one with the highest index) is "main". This
variable exists only when a shell function is executing.
Assignments to FUNCNAME have no effect and return an error
status. If FUNCNAME is unset, it loses its special
properties, even if it is subsequently reset.

This variable can be used with BASH_LINENO and
BASH_SOURCE. Each element of FUNCNAME has
corresponding elements in BASH_LINENO and BASH_SOURCE
to describe the call stack. For instance,
${FUNCNAME[$i]} was called from the file
${BASH_SOURCE[$i+1]} at line number
${BASH_LINENO[$i]}. The caller builtin displays the
current call stack using this information.

除了 $BASH_SOURCE 外,還有哪些變數可以使用呢?

變數於下述,至於變數值的說明可見此篇文件:Bash Variables

  • BASH
  • BASHOPTS
  • BASHPID
  • BASH_ALIASES
  • BASH_ARGC
  • BASH_ARGV
  • BASH_CMDS
  • BASH_COMMAND
  • BASH_COMPAT
  • BASH_ENV
  • BASH_EXECUTION_STRING
  • BASH_LINENO
  • BASH_LOADABLES_PATH
  • BASH_REMATCH
  • BASH_SOURCE
  • BASH_SUBSHELL
  • BASH_VERSINFO
  • BASH_VERSION
  • BASH_XTRACEFD
  • CHILD_MAX
  • COLUMNS
  • COMP_CWORD
  • COMP_LINE
  • COMP_POINT
  • COMP_TYPE
  • COMP_KEY
  • COMP_WORDBREAKS
  • OMP_WORDS
  • COMPREPLY
  • COPROC
  • DIRSTACK
  • EMACS
  • ENV
  • EUID
  • EXECIGNORE
  • FCEDIT
  • FIGNORE
  • FUNCNAME
  • FUNCNEST
  • GLOBIGNORE
  • GROUPS
  • HISTCMD
  • HISTCONTROL
  • HISTFILE
  • HISTFILESIZE
  • HISTIGNORE
  • HISTSIZE
  • HISTTIMEFORMAT
  • HOSTFILE
  • HOSTNAME
  • HOSTTYPE
  • IGNOREEOF
  • INPUTRC
  • LANG
  • LC_ALL
  • LC_COLLATE
  • LC_CTYPE
  • LC_MESSAGES
  • LC_NUMERIC
  • LC_TIME
  • LINENO
  • LINES
  • MACHTYPE
  • MAILCHECK
  • MAPFILE
  • OLDPWD
  • OPTERR
  • OSTYPE
  • PIPESTATUS
  • POSIXLY_CORRECT
  • PPID
  • PROMPT_COMMAND
  • PROMPT_DIRTRIM
  • PS0
  • PS3
  • PS4
  • PWD
  • RANDOM
  • READLINE_LINE
  • READLINE_POINT
  • REPLY
  • SECONDS
  • SHELL
  • SHELLOPTS
  • SHLVL
  • TIMEFORMAT
  • TMOUT
  • TMPDIR
  • UID

相關網頁

Save

關於 Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.
本篇發表於 Programming 並標籤為 , , , , , 。將永久鏈結加入書籤。

發表迴響