2017-04-13 5 views
0

最初の引数が返された関数tを定義しました。始まりにはうまくいくようです。しかし、私が昼食のために休憩した後、私はtを何も引数なしで思い出します。戻りコードは127で、私の期待通りではありません。0です。その後、私は最後のリコール時には期待通りに動作します。bashの奇妙なリターンコードの振る舞い

これは非常に奇妙です。それはどうなりますか?それとも私は何か悪いことをしましたか?

$ t; echo $? ## the 1st time I call function t, its returncode is 127 
127 
$ t 3; echo $? 
3 
$ t; echo $? ## the 2nd time I call function t, its returncode is 0 
0 
$ t; echo $? ## the 3rd time I call function t, its returncode is 0 
0 
$ declare -f t 
t() 
{ 
    return $1 
} 
$ 
$ echo $SHELL 
/bin/bash 
$ bash --version 
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu) 
Copyright (C) 2005 Free Software Foundation, Inc. 
$ lsb_release -a 
LSB Version: :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch 
Distributor ID: CentOS 
Description: CentOS release 5.11 (Final) 
Release:  5.11 
Codename:  Final 
+1

値がわからない場合は、常に変数を引用してください: 'return" $ 1 "'それはあなたにエラーをもたらしました。 – ceving

答えて

1

$1は未定義と引用符で囲まれていないで、あなたの関数は、ちょうどreturnを実行します。

これは前のコマンドの結果、つまり引数なしでtを実行する前に終了したコマンドの結果を返します。