同じ式の複数の変数拡張修飾子

同じ式の複数の変数拡張修飾子

bash 4.1.0で次のイディオムが機能しないのはなぜですか?

if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]]

これは文脈上...

function isCircularRef_test () {
  #
  ### Seems like this should work but it does not.
  ###   if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]]; then ...
  ### It appears to fail silently and exit the script so neither 'then' nor
  ### 'else' branch executes.
  ### Storing the array into a temporary string variable works. Why necessary?
  #
  printf "%s\n" "VERSION #1"
  local _fna="${FUNCNAME[*]:1}"
  if [[ "${_fna/$FUNCNAME/}" != "${_fna}" ]]
  then
    printf "%s\n" "IS circular reference"
  else
    printf "%s\n" "IS not circular reference"
  fi
  #
  printf "%s\n" "VERSION #2"
  if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]]
  then
    printf "%s\n" "IS circular reference"
  else
    printf "%s\n" "IS not circular reference"
  fi
}

出力は...

VERSION #1
IS not circular reference
VERSION #2

ベストアンサー1

これ文書シェルパラメータ拡張表現の代替構文は次のとおりです。

${parameter/pattern/string}

最初の部分は、parameterパラメータ拡張を含む別の式ではありません。他のすべての拡張修飾子にも同様に適用されます。次の 2 つの手順で行う必要があります。

func1=${FUNCNAME:1}
if [[ ${func1/$FUNCNAME/} != ${func1} ]]

おすすめ記事