Development

How to check if a command succeeded in Linux

Captain Salem 2 min read

How to check if a command succeeded in Linux?

Oh the terminal, we are all familiar with the Linux terminal. Maybe a little too when. When executing commands in the Linux terminal, you may come across instances where you need to determine whether a command executed successfully.

This is especially useful in automated commands such as bash scripts or cron jobs where the execution of a previous command can impact the execution of the rest of the program.

In this tutorial, we’ll explore various methods of checking if a command succeeded focusing primarily on Unix-based systems.

Checking If a Command Succeeded in Unix-like Systems

Unix-like systems such as Linux and MacOS, use a mechanism called exit status to indicate whether a command has succeeded or not.

The exit status is an integer value where 0 indicates success, and any other number between 1 and 255 indicates a failure. This status is stored in a special variable known as $?

Consider the examples below that demonstrates the usage of this variable.

touch test.txt
echo $?

In the above example, the touch command creates a new file named test.txt.

We then use the echo $? command then prints out the exit status of the touch command. If the file is successfully created, echo $? will output 0.

This mechanism can be very useful in shell scripts. For instance, you can perform different actions depending on whether a command succeeds or fails.

Consider the example shell script that uses the $? variable:

#!/bin/bash
touch test.txt

# Check if it succeeded
if [ $? -eq 0 ]
then
  echo "The command succeeded."
else
  echo "The command failed."
fi

In the example, we start by attempting to create a file with the specified name. If the command succeeds, it will print the message corresponding to the error.

As you can guess, you can expand this feature to ensure more complex logic and error-handling mechanisms.

Conclusion

In this tutorial, we explored how you can check whether a previous command executed successfully in Linux and other Unix-base systems.

The command prompt or the Windows PowerShell is the terminal equivalent in the Unix world. Similarly, you may come across instances where you need to determine whether a command executed successfully.

This is especially useful in automated commands such as PowerShell and Batch scripts where the execution of a previous command can impact the execution of the rest of the program.

In this tutorial, we’ll explore various methods of checking if a command succeeded focusing primarily on Windows systems.

Checking If a Command Succeeded in Windows

In Windows, you can use the the exit status still exists like that in Unix-systems. However, instead of $? (unless it’s PowerShell), we use the variable %errorlevel% to check if a command succeeded.

Consider the example below:

C:\> mkdir dir
C:\> echo %errorlevel%
0

In this example, the mkdir command creates a new directory named dir.

The echo %errorlevel% command then prints out the exit status of the mkdir command. If the directory is successfully created, echo %errorlevel% will output 0.

Just like in Unix-like systems, you can use this mechanism in batch scripts to perform different actions depending on whether a command succeeds or fails.

Here’s an example of a batch script that uses the %errorlevel% variable:

@echo off

:: Execute a command
mkdir dir

:: Check if it succeeded
if %errorlevel% equ 0 (
  echo The command succeeded.
) else (
  echo The command failed.
)

This script attempts to create a directory. If the command succeeds, it will print “The command succeeded.”

If it fails (perhaps because you don’t have permission to create a directory in the current location), it will print “The command failed.”

Conclusion

Knowing how to check if a command succeeded can be incredibly useful when dealing with command-line interfaces, particularly in situations where automation and scripting are involved.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.