简体   繁体   中英

strange returncode behavior in bash

I defind a function t which just return the 1st argument passed to it. It seems work fine at the beganing. But after I take a rest for lunch, I recall t without passing any arguments. It return code is 127 at 1st, not as my expecting 0 . After that it works fine as I expecting at last recallings.

That's very strange. What happens in it? Or did I do something wrong?

$ 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

When $1 is undefined and unquoted, your function just runs return .

This returns the result of the previous command, ie whatever command exited before you ran t without arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM