Bash Logging: Does Log Once Hide Live Failures?
In this article
Bash's log once option does not hide live failures; it suppresses repeated logging of the same error condition after the first occurrence, while the failure itself continues to be handled and can still be observed through other means. This article explains exactly how log once works, when it can mask ongoing issues, and how to configure it to avoid missing critical failures.
What Does "log once" Actually Do?
In Bash, log once is a shell option (enabled with shopt -s log_once) that controls how the shell reports errors from certain built-in commands, particularly cd and printf. When enabled, the shell prints an error message only the first time a specific command fails in a given shell session. Subsequent failures of the same command type are silently ignored—no error message is printed to stderr.
For example, if you run cd /nonexistent twice in a session with log_once enabled, the first attempt prints bash: cd: /nonexistent: No such file or directory, but the second attempt prints nothing. The command still fails (the exit status is still non-zero), but the shell does not repeat the error message.
Does It Hide Live Failures?
Technically, log once does not hide the failure itself—the command still returns a non-zero exit status, which scripts can check. However, it does hide the visual notification of that failure from the user. If you rely on reading error messages in the terminal to notice problems, log once can make it appear that a command succeeded when it actually failed repeatedly. This is especially dangerous in interactive sessions where you might not check $? after every command.
In scripts, the impact is minimal because scripts typically check exit statuses explicitly. But in interactive use, log once can mask a recurring issue (e.g., a network drive that keeps disconnecting) after the first error, making it easy to miss that the problem persists.
How to Enable and Disable It
To enable log once, use:
shopt -s log_once
To disable it, use:
shopt -u log_once
You can check its current status with:
shopt log_once
The option is off by default in most Bash installations. It is not a standard POSIX option, so it may not be available in all shells (e.g., dash or zsh have different mechanisms).
When Is It Useful?
log once is intended to reduce noise when a command fails repeatedly in a loop or when a user is doing something that generates many identical errors. For example, if you have a script that tries to cd into a directory that is temporarily unavailable, you might want to see the error only once rather than hundreds of times.
It is also useful in interactive sessions where you accidentally type a wrong path repeatedly—the first error is enough to alert you, and subsequent identical errors are just clutter.
Common Misconceptions and Pitfalls
- Misconception: It suppresses all errors. It only suppresses repeated errors of the same type (specifically from
cdandprintf). It does not affect other commands likelsorgrep. - Misconception: It affects exit status. No, the exit status is still non-zero on failure. The option only affects the printed message.
- Pitfall: It can hide a persistent failure. If a command fails once, then later fails again after you've fixed something, you won't see the new error message because the shell remembers that this command type already failed.
- Pitfall: It is session-wide. The suppression applies to the entire shell session, not just a specific command or script. Once a command type fails, it will not print again until the shell exits.
How to Avoid Missing Live Failures
If you want the noise reduction but still want to know when a failure occurs, consider these alternatives:
- Check exit status in scripts: Always test
$?or useset -eto exit on error. - Use a custom error handler: Define a trap for
ERRto log errors to a file, so you have a record even if the terminal output is suppressed. - Redirect stderr to a log: Instead of relying on the terminal, send stderr to a file and tail it when needed.
- Disable
log oncefor critical sessions: If you are debugging a live issue, turn it off withshopt -u log_once.
Frequently Asked Questions
Does log once affect the exit code of a command?
No. The exit code is determined by the command itself. log once only controls whether the shell prints an error message to stderr. The command still returns a non-zero status on failure, which you can capture with $?.
Can I reset the "once" counter without restarting the shell?
No, there is no built-in way to reset the counter. The suppression lasts for the entire shell session. You would need to start a new shell or disable the option (which does not reset the counter, but allows future errors to print).
Does log once work in scripts?
Yes, it works in scripts, but it is rarely useful there because scripts usually check exit statuses. However, if a script runs a failing command in a loop, the first error will print, and subsequent ones will be silent—which might be desirable to reduce log spam.
Is log once the same as suppressing all errors?
No. It only suppresses repeated error messages for the same command type (specifically cd and printf). Other commands still print their errors normally. It is not a global error suppressor.
What if I want to see the first error but not the rest?
That is exactly what log once does. If you want to see the first error and then suppress the rest, enable it. If you want to see all errors, keep it disabled.
Is log once available in all Bash versions?
It has been available since Bash 3.0, so it is present in virtually all modern systems. However, it is not a POSIX feature, so some minimal shells may not have it.
Conclusion
log once is a useful tool for reducing repetitive error messages, but it comes with the risk of hiding persistent failures. Understanding exactly what it suppresses—and what it doesn't—allows you to use it safely. For critical operations, always rely on exit status checks rather than visual error messages, and consider using a log file to capture all errors even when log once is enabled.