You can’t reliably call logout inside a bash script because it only works in an interactive login shell, not in a non‑interactive script. The practical workarounds are to use exit to end the script, kill the parent shell, or structure your script to be sourced so that logout can act on the current shell.
Why logout Fails in Scripts
Bash has two distinct modes: interactive and non‑interactive. A script runs in non‑interactive mode, meaning it doesn’t read from a terminal and doesn’t have the same built‑in commands. logout is a built‑in that only makes sense in a login shell—a shell that was started with --login or as a login shell by the system. In a script, bash simply reports logout: not login shell and continues. The command isn’t a program on disk; it’s a shell built‑in, so there’s no external binary to call either.
What exit Does and When to Use It
The most common workaround is to use exit instead. exit terminates the current shell process. If your script is run as a child process (e.g., ./script.sh), exit ends just the script, not your terminal session. That’s usually what you want—you don’t want to log the user out when they run a script from their terminal.
exitwith no argument returns the exit status of the last command.exit 0indicates success;exit 1(or any non‑zero) indicates an error.- In a sourced script (run with
source script.shor. script.sh),exitwill exit the entire shell, because the script runs in the same shell process.
So if your goal is simply to stop the script at a certain point, exit is the correct tool.
Killing the Parent Shell from a Script
If you genuinely need to log out the user’s shell from within a script, you can send a signal to the parent process. The script can find its parent shell’s PID using $PPID and then kill it:
kill -HUP $PPID
Sending SIGHUP (hangup) to the parent shell typically causes it to terminate, which effectively logs the user out if that shell is the login shell. However, this is a blunt instrument—it will kill the shell regardless of what the user is doing, and it may not cleanly end the session. A more graceful approach is to use pkill -u $USER to terminate all processes belonging to the user, but that can be dangerous if other important processes are running.
Sourcing the Script to Use logout
If you must use logout itself, you need to run the script in the current shell by sourcing it. When you source a script, it executes in the same shell process, and if that shell is a login shell, logout will work. For example:
source /path/to/script.sh
But beware: if the script contains logout, it will log out the user immediately, with no confirmation. This is rarely what you want in a script meant to be run manually. It’s more common to use exit and let the user decide when to log out.
Common Problems and Misconceptions
One common mistake is assuming logout works like exit but with extra features. It doesn’t—it’s specifically tied to login sessions. Another is trying to call logout via bash -c or in a subshell; that will fail because the subshell isn’t a login shell. Also, some users try to use exec logout thinking it will replace the script process with the logout command, but logout is a built‑in, so exec can’t find an executable file named logout.
Another misconception is that exit will always log out the user. That’s only true if the script is sourced. If you run ./script.sh and it contains exit, the terminal stays open. This is actually a feature—it lets you run scripts without fear of losing your session.
How to Choose the Right Workaround
Decide based on what you want to achieve:
- Stop the script only → use
exit. - Log out the user from a non‑interactive script → use
kill -HUP $PPID(with caution). - Use
logoutitself → source the script from a login shell. - End the script and also close the terminal → use
exitin a sourced script, orkillthe terminal process.
For most automation tasks, exit is sufficient. Logging out the user is rarely something you want a script to do automatically, because it can disrupt work and cause data loss.
Frequently Asked Questions
Can I use logout in a bash script if I run it with sudo?
No. sudo runs the script as another user, but the shell that executes the script is still non‑interactive and not a login shell. logout will still fail with the same error.
What’s the difference between exit and logout in a terminal?
In an interactive login shell, logout ends the session and closes the terminal. exit also ends the shell, but it can be used in any shell, not just login shells. In a non‑login interactive shell (like a terminal opened from a window manager), logout is not available, and exit is the only option.
How do I log out a user from a script without killing their processes?
You can’t fully log out without ending the login shell, which will terminate the processes that belong to that session. If you want to avoid killing background jobs, you can send SIGHUP to the parent shell, but that will still terminate the shell and its children. There’s no way to log out a user and keep their processes running—that’s what nohup or disown are for, but they don’t prevent logout from killing the shell.
Why does logout work in a script when I source it?
When you source a script, it runs in the current shell. If that shell is an interactive login shell, logout is a valid built‑in and will terminate the shell. The script’s commands are executed as if you typed them, so the shell’s context is preserved.
Is there a way to make logout work in a non‑interactive script?
No, not directly. The bash built‑in logout checks whether the shell is a login shell. You could create a new login shell inside the script and run logout there, but that would only log out that new shell, not the user’s original session. It’s simpler to use kill or exit.
What is the exit status of a script that uses kill -HUP $PPID?
The script itself will exit with the status of the kill command, which is 0 on success. However, the parent shell is terminated, so the script’s exit status may not be captured by anything. If you need to report something, use exit 0 after the kill, but the parent is already gone.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Turn Off Backup Encryption in iTunes?
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It