Programs typically buffer their output for efficiency. That is, they accumulate output in a memory area (called a buffer), and they actually get the output out only when the buffer is full or at certain key points in the program. When the program ends normally, it flushes the output buffer (i.e. prints out any data that's left in it). When it segfaults, the content of the buffer is lost.
You don't observe this effect when running the program directly in a terminal because the behavior is different when the program's output is connected to a terminal (as opposed to a regular file or a pipe). In a terminal, the default behavior is to flush the buffer at the end of each line. Therefore you'll see every complete line that's produced up to the point when the program segfaults.
You can force the program to run in a terminal and collect its output. The simplest way is to run script
. There are a number of annoyances that you'll need to work around:
script
adds a header line to the transcript file, which you'll need to remove afterwards.
script
doesn't return the status code of the command, so you'll need to save it somewhere if you want to know about the segfault or any other error.
script
will cause normal output and error out; you'd better save the error output to a separate file.
export FONT="foo"
script -q -c '
ttf2afm "$FONT.ttf" 2>"$FONT.ttf2afm-err";
echo $? >"$FONT.ttf2afm-status"
' "$FONT.ttf2afm-typescript"
tail -n +2 <"$FONT.ttf2afm-typescript" >"foo.afm"
rm "$FONT.ttf2afm-typescript"
if [ "$(cat "$FONT.ttf2afm-status")" -ne 0 ]; then
echo 1>&2 "Warning: ttf2afm failed"
cat "$FONT.ttf2afm-err"
fi
Bash built-ins are inconsistent and poorly documented.
Here's an example:
$ help command
command: command [-pVv] command [arg ...]
Runs COMMAND with ARGS ignoring shell functions. If you have a shell
function called 'ls', and you wish to call the command `ls', you can
say "command ls". If the -p option is given, a default value is used
for PATH that is guaranteed to find all of the standard utilities. If
the -V or -v option is given, a string is printed describing COMMAND.
The -V option produces a more verbose description.
$ command; echo $?
0
Even without command
the return code $? -eq 0
and there is no error on std err
.
Another one:
$ help disown
disown: disown [-h] [-ar] [jobspec ...]
By default, removes each JOBSPEC argument from the table of active jobs.
If the -h option is given, the job is not removed from the table, but is
marked so that SIGHUP is not sent to the job if the shell receives a
SIGHUP. The -a option, when JOBSPEC is not supplied, means to remove all
jobs from the job table; the -r option means to remove only running jobs.
$ disown; echo $?
-bash: disown: current: no such job
1
All the arguments are optional but it returns $? -eq 1
when there are none.
I've even compiled the newest Bash 4.2 and here are my results:
$ help command
command: command [-pVv] command [arg ...]
Execute a simple command or display information about commands.
Runs COMMAND with ARGS suppressing shell function lookup, or display
information about the specified COMMANDs. Can be used to invoke commands
on disk when a function with the same name exists.
Options:
-p use a default value for PATH that is guaranteed to find all of
the standard utilities
-v print a description of COMMAND similar to the `type' builtin
-V print a more verbose description of each COMMAND
Exit Status:
Returns exit status of COMMAND, or failure if COMMAND is not found.
$ command; echo $?
0
There's a new section "Exit Status" and command
is still an optional argument. Even worse than 3.x. The same for other built-ins.
So, you're right. Bash built-ins are a mess and should be fixed.
Best Answer
The
cd
command modifies the "current working directory", right?"current working directory" is a property that is unique to each process.
So, if
cd
was a program it would work like this:cd foo
cd
process startscd
process changes the directory for the cd processcd
process exits