Summary

The Linux logout command doesn’t work inside Bash scripts. It’s rare but, occasionally, you may need that functionality. Here are three ways to sidestep the issue and log out from a script.

Why logout Doesn’t Work in Scripts

There are login shells and non-login shells. The very first shell that is created when you log in is a login shell. All other shells are not login shells. The differences between the two types of shell stem from the different system files that are read by the shell as it is created.

Plainly, you can’t log out of a non-login shell. If a shell has no concept of logging in, it can’t log you out.

Running the logout command on a computer without a graphical desktop environment installed.

Shell scripts don’t run in the shell they’re launched from. A new shell is spawned for them to run in. Even if they were launched from a login shell, they don’t run in a login shell.

We can see this quite easily. This is the login prompt of a computer running Arch Linux, without any graphical desktop environment installed. It’s using theBash shell. I’ve logged in, and I’m about to hit Enter on the logout command.

The login prompt on a computer without a graphical desktop environment installed.

As you’d expect, I’m dumped back at the login prompt.

So evidently, the shell I’m using is a login shell. Let’s log back in, and check the contents of a script called lo.sh. Then we’ll run it.

Bash complains that we can’t use logout in a non-login shell. Even though our script was definitely launched from a login shell, it doesn’t work. We’re not logged out.

Running a script with the logout command in it, from a login shell.

Nor will it work in aterminal emulator windowon a computer with a graphical desktop environment. In this case, it’s Ubuntu with the GNOME desktop.

I said that you only have a single login shell. That’s not strictly the case. There are scenarios where you may have more than one login shell running on your computer, but you only have a single login shell per user instance.

Trying to use the logout command in a terminal window with a non-login shell.

Each user needs their own login shell, so they can log in and out. It’s possible to log in to the same computer, more than once, as the same user. If you have SSH set up on your computer to support remote access, you can log in again over SSH, to your local computer, through a terminal window.

Once we’ve logged in, we run the who command to list the current users, then log out and run who again. We can see that we temporarily had an extra user. That user has their own login shell. But that doesn’t help us with our scenario. They’re the only user who can access that shell, and they’ll see the same behavior as we’ve just seen.

Using SSH to log in to the local computer.

How To logout Using exec

So: logout must be used in a login shell, but our script won’t run in a login shell even if it is launched from one.

What we can do is launch our script with exec. This doesn’t follow the normal process of creating a shell to run the script in. It replaces the current shell with the command that exec launches. The shell is discarded. When the command terminates, it can’t go back to the now non-existent shell. If we do this from a login shell, we’ll be logged out.

Using the chmod command to make the lo.sh script executable.

We need a script to experiment with. Copy this trivial script to your favorite editor, then save it as “lo.sh”.

We’ll usechmodto make it executable.

The script fails with the now familiar error message:

If we launch it with exec, the terminal window is closed when the script stops running.

Of course, on our console-only Arch computer, we’re not using a terminal emulator. exec replaces our login shell with the script.

Running a script with the logout command in it, in a terminal window with a non-login shell.

When the script finishes, we’re presented with a new login prompt. So, it works for console logins on Linux computers without a graphical desktop environment installed. Does that help us on our Ubuntu computer that’s running GNOME?

Yes, it does. In GNOME, we can press Ctrl+Alt+FnKey to open exactly the same sort of full screen, login shell console. The FnKey can be one of F3 through F6.

Using exec to launch a script that has logout in it, in a non-login shell.

We can log in and use the same command as we did on our Arch computer.

We are logged out, and returned to the console login prompt. Pressing Ctrl+Alt+F1 returns you to your GNOME session. You’ll need to enter your password to regain access.

Logging in to a console login shell on a computer running GNOME, and using exec to launch a script with logout in it.

Note that you’re returned to your GNOME session the way it was before you pressed Ctrl+Alt+F3. Your GNOME session isn’t logged out, but it is password protected.

If you don’t want to have your GNOME session user left logged in, log them out before you run your script. you may press Ctrl+Alt+F3 on the Ubuntu login screen, sign in at the console, and run your script.

The Ubuntu GNOME login screen.

A couple of points. When you’re using exec like this, the logout command in the script is superfluous. It’s the fact that the script is terminating with no shell to go back to that effectively logs you out.

You could use exit instead of logout, or not use either command at all. Once the script terminates, you’re logged out. If you’ve got branches of execution in your script, and it can terminate in different places, you may use exit to terminate each of the possible paths of execution.

Another issue is, because you’re not executing your script inside a shell, you don’t have access to shell features like the expansion of wildcards, nor aliases and shell functions. If your script needs these, you may use one of the other techniques, below.

How To logout Using source

Using the Linux source command interprets the commands inside your script without launching the script in a shell. The commands are interpreted by the current shell.

Open a console window as above, using Ctrl+Alt+F3, and use the source command to interpret the commands in your script.

You can use the word source, or a period.

With this technique you’ll need to use exit or logout the script. The termination of the script on its own won’t log you out.

How to logout Using a Bash Function

There’s a way to run any script and have it log out when it finishes, without calling logout or exit from the script itself. Save this script as simple.sh.

Make it executable by typing:

In a login console screen, add the logout command after the script name. Separate the script name from the logout command with a semicolon.

When our script terminates, we’re logged out.

If you do this frequently, it’d be convenient to make a small Bash function like this.

You can call this function and pass the name of a script to it. When you do this in a login console, you’ll be logged out when the script finishes.

A Quick Round-Up

If you want to have a script running to completion in your absence, and have the user logged out when it completes so there are no user logged in at all, follow these steps.