2017-05-13 2 views
2

シェル関数内で組み込みを呼び出すときに再帰を防止: - .bashrcのリロード後は、私はbashrcに、関数の下に追加

function cd(){ 
     echo "user switching to" $1 
     cd $1 
} 

、tempディレクトリ(任意のディレクトリ)内のCDへの試みは、再帰的なエラーの下に与える: -

user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
bash: echo: write error: Bad address 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 
user switching to temp/ 

ユーザがcdコマンドを使用するたびに、ロジックを実行したいので、メソッド名をcdにします。

+1

FYI、 'function'キーワードは非標準であり、あなたのコードの移植性を制限します。すべてのPOSIXシェルで作業するには、それを削除して 'cd(){'で始まる関数を定義してください。 –

答えて

0

あなたはbashでbuiltinコマンドを使用することができます。

function cd(){ 
     echo "user switching to $1" 
     builtin cd "[email protected]" 
} 
+1

'cd'には限られた数のオプションもあります。 '$ 1'は必ずしも新しい作業ディレクトリの名前ではありません。 – chepner

+3

'builtin cd" $ @ "'はより堅牢です。 –

関連する問題