If you want to be sure that the command you executed on a Bash Shell did complete successfully or failed then you can make use of the below syntax.
Oneliner Syntax:command && echo "The command was successful" || echo "The command failed"
Example:
bash-3.2$ whoami && echo "The command was successful" || echo "The command failed"
c2ctechtv
The command was successful
If this seems too long, you can use -1 for failure and 1 for success.
Example:
command && echo 1 || echo -1
If this is too much, then we can create an alias function that can make this super simplified.
Alias:alias cstatus='function _cstatus() { "$@" && echo "Command succeeded" || echo "Command failed"; }; _cstatus'
Now if you run any command with the prefix cstatus, you will get a string to indicate if the command succeeded or failed.
Example:
bash-3.2$ cstatus whoami
c2ctech
Command succeeded
bash-3.2$ cstatus whoamaa
bash: whoamaa: command not found
Command failed
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Bash,- How to use Autocomplete and Autosuggestion in Shell Commands
- Bash How to Save Output of a Command to a Variable
- How to know the current shell you are logged in?
- How to Echo Bash Command to a File
- Bash Command to Get Absolute Path for a File
- How to Split a String based on Delimiter in Bash Scripting
- Bash: Command Line Arguments to Bash Script Examples
- Bash Command to Download a File From URL
- How to check if a Command Exists using Bash Script
- Ways to Increment a counter variable in Bash Script
- Know Bash shell version command
- Bash command to Read, Output and Manipulate JSON File
- Bash Command to Base64 Decode a String
- Bash Command to Check Python Version
- Bash: Command to Find the Length of a String
- What is $$ in Bash Shell Script- Special Variable
- Bash - How to check if a Command Failed?
- List all Username and User ID using Bash Command
- Command to Sort File In Reverse Order [Unix/Linux/macOS]
- bash: netstat: command not found
- Bash Command To Go Back To Previous Directory
- [Fix] bash: script.sh: /bin/bash^M: bad interpreter: No such file or directory
- How to check your IP using bash for Windows?
- Bash Command To Check If File Exists
- Convert String from uppercase to lowercase in Bash
More Posts: - How to Increase MacBook Trackpad Cursor Speed on macOS Ventura/Sonoma - MacOS
- Center Align Android TextView Text - Android
- How to Right Click on Mac Desktop? - MacOS
- SwissCovid App Launched in Switzerland - News
- Zsh Shell: Custom alias that you may find useful - MacOS
- How to Split a Java Collections using Predicate, Lambda and Streams - Java
- How to install iTerm2 Mac Terminal Alternative - MacOS
- How to find the installed version of Microsoft Teams - Teams
bash-3.2$ cstatus whoami
c2ctech
Command succeeded
bash-3.2$ cstatus whoamaa
bash: whoamaa: command not found
Command failed
![Check if a Bash Command Failed with Example Check if a Bash Command Failed with Example](https://code2care.org/howto/bash-check-if-command-failed/images/Check if a Bash Command Failed with Example.jpg)
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- How to use Autocomplete and Autosuggestion in Shell Commands
- Bash How to Save Output of a Command to a Variable
- How to know the current shell you are logged in?
- How to Echo Bash Command to a File
- Bash Command to Get Absolute Path for a File
- How to Split a String based on Delimiter in Bash Scripting
- Bash: Command Line Arguments to Bash Script Examples
- Bash Command to Download a File From URL
- How to check if a Command Exists using Bash Script
- Ways to Increment a counter variable in Bash Script
- Know Bash shell version command
- Bash command to Read, Output and Manipulate JSON File
- Bash Command to Base64 Decode a String
- Bash Command to Check Python Version
- Bash: Command to Find the Length of a String
- What is $$ in Bash Shell Script- Special Variable
- Bash - How to check if a Command Failed?
- List all Username and User ID using Bash Command
- Command to Sort File In Reverse Order [Unix/Linux/macOS]
- bash: netstat: command not found
- Bash Command To Go Back To Previous Directory
- [Fix] bash: script.sh: /bin/bash^M: bad interpreter: No such file or directory
- How to check your IP using bash for Windows?
- Bash Command To Check If File Exists
- Convert String from uppercase to lowercase in Bash
- How to Increase MacBook Trackpad Cursor Speed on macOS Ventura/Sonoma - MacOS
- Center Align Android TextView Text - Android
- How to Right Click on Mac Desktop? - MacOS
- SwissCovid App Launched in Switzerland - News
- Zsh Shell: Custom alias that you may find useful - MacOS
- How to Split a Java Collections using Predicate, Lambda and Streams - Java
- How to install iTerm2 Mac Terminal Alternative - MacOS
- How to find the installed version of Microsoft Teams - Teams