James Wright
James Wright

Reputation: 1435

Why does zsh hide error output from a c program?

I have a program written in C that fails. When I run it in zsh, the program fails, but it's error output is not displayed in command line (note that the second ➜ was red, indicating a failed execution):

hpsc-hw2-USER on  master 
➜ ./sort -a 405500

hpsc-hw2-USER on  master 
➜ 

However, when I run it in bash, the error shows up:

[USER@COMPUTER hpsc-hw2-USER]$ ./sort -a 405500
Segmentation fault (core dumped)

Any idea why? I'm running oh-my-zsh with the spaceship theme if that helps.

Upvotes: 2

Views: 2277

Answers (2)

Caleb
Caleb

Reputation: 125007

Any ideas how to make zsh output the error message?

The result of the last command is stored in the shell variable ?, so you can print it with something like print $? or echo $?. Like most shells, zsh is incredibly configurable, so you can write a script that e.g. includes the result of the last command in your prompt. Here's a blog post in which the author configures zsh to display non-zero results in the right prompt: Show Exit Code of Last Command in Zsh.

Or why it doesn't do it to begin with?

Shell commands don't normally crash, and if they encounter errors they typically provide more useful output than just the return code on their own. The return code is most useful for scripts, so that you can write a script that handle error conditions.

Upvotes: 1

klutt
klutt

Reputation: 31409

but it's error output is not displayed in command line

That's not something that your program writes to the screen. It's the shell that's writing it. So the shell is not 'hiding' it.

I'm not using zsh myself, but since a red arrow indicates that the program was abnormally terminated, I guess you can look at the code for the red arrow and create a custom message. Here is a question about the error codes that might help you: What error code does a process that segfaults return?

I remember that I once made a custom bash prompt that showed the last exit code. Maybe I used this, but I'm not sure: Bash Prompt with Last Exit Code

Here is the documentation for how to customize the prompt for spaceship theme: https://denysdovhan.com/spaceship-prompt/docs/Options.html#exit-code-exit_code

I assume that you need to add exit_code to the SPACESHIP_PROMPT_ORDER section in your .zshrc file.

Any idea why?

You probably have to ask the developers. An equally valid question is: "Why does bash print 'segmentation fault'?" It's just a design choice. Bash does not print the memory address where the segfault occurred. Why is that?

Seems like the developers of oh-my-zsh thought it was enough with a red arrow.

Upvotes: 1

Related Questions