[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
value of $? and exit status are partially undocumented, possibly wrong
From: |
Vincent Lefevre |
Subject: |
value of $? and exit status are partially undocumented, possibly wrong |
Date: |
Fri, 13 Oct 2023 15:18:21 +0200 |
User-agent: |
Mutt/2.2.12+67 (757ca3b3) vl-149028 (2023-09-09) |
With bash 5.2.15(1)-release under Debian (bash 5.2.15-2+b6 package),
on one of my machines:
$ bash -c 'echo $?'
0
$ bash
vlefevre@cventin:~$ echo $?
0
$ bash ; echo $?
vlefevre@cventin:~$ exit
exit
0
and on another one:
$ bash -c 'echo $?'
0
$ bash
vinc17@zira:~$ echo $?
1
$ bash ; echo $?
vinc17@zira:~$ exit
exit
1
The difference seems to be due to the exit status of the last command
of the .bashrc file. But this is undocumented.
Note that on the opposite, for a login shell, e.g. "bash -l", the
exit status of the last command of .bash_logout is ignored for the
exit status of bash. So this is confusing.
Moreover, for "bash -l /dev/null", when .bash_profile ends with a
non-zero exit status, the behavior contradicts
ARGUMENTS
If arguments remain after option processing, and neither the -c nor
the -s option has been supplied, the first argument is assumed to be
the name of a file containing shell commands. If bash is invoked in
this fashion, $0 is set to the name of the file, and the positional
parameters are set to the remaining arguments. Bash reads and
executes commands from this file, then exits. Bash's exit status is
the exit status of the last command executed in the script. If no
^^^^^
commands are executed, the exit status is 0. An attempt is first
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
made to open the file in the current directory, and, if no file is
found, then the shell searches the directories in PATH for the
script.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
- value of $? and exit status are partially undocumented, possibly wrong,
Vincent Lefevre <=