異常な状況で組み込みコマンドが実行されているか確認

異常な状況で組み込みコマンドが実行されているか確認

次のような過酷な環境を考えてみましょう。

for word in builtin command type unfunction declare set unset alias; do
    eval "$word(){ echo $word function; };  alias $word='echo $word alias'"
done 

組み込みコマンドにアクセスできますか?たとえば、次のようなことは可能ですか?

\command \builtin \type echo

まだ見ることができるもの:

echo is a shell builtin

敵対的な(またはロックされた)環境でデフォルトの組み込み、関数、またはパス名に絶対にアクセスできないように物事を完全に上書きすることは可能ですか?

ベストアンサー1

Bashの場合、有効にするとPOSIXモード:

  1. 関数名は、POSIX 特殊組み込み関数の 1 つと異なる場合があります。
  2. POSIX 特殊組み込み関数は、コマンド照会中にシェル関数の前に見つかります。

とは特殊な組み込み関数なので、POSIXスキーマを介してアクセスし、それを使用して関数を削除してエイリアスを削除するために使用できますsetunset\unalias

$ for word in builtin command type  declare set unset unalias alias; do     eval "$word(){ echo $word function; };  alias $word='echo $word alias'"; done 
alias function
$ unalias  builtin command type unfunction declare set unset alias
unalias alias builtin command type unfunction declare set unset alias
$ POSIXLY_CORRECT=1
$ \unset -f builtin command type  declare set unset unalias alias
$ \unalias  builtin command type unfunction declare set unset alias
bash: unalias: unfunction: not found
bash: unalias: alias: not found

$ type -a builtin
builtin is a shell builtin

しかし、次のように戻す方法は思い出されません。

enable -n set unset builtin command type  declare unalias alias typeset enable shopt

(制限されたシェルでは許可されない可能性があるため、新しいシェルを起動する必要はありません。)

おすすめ記事