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 Mac Terminal Buffer Size - MacOS
- How to Search Something (string) in Android Studio Project like Eclipse - Android-Studio
- TextEdit Clear Formatting when Pasting Text - MacOS
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED - Java
- [IRCTC] Indian railways official eRail API 1.1 for developers to get train info - HowTos
- How to close tabs on Notepad++ on Click? - NotepadPlusPlus
- SharePoint Online REST API not returning all list items and limit to only 100 rows - SharePoint
- How to find the path of formula Installation by Brew - HowTos
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!
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 Mac Terminal Buffer Size - MacOS
- How to Search Something (string) in Android Studio Project like Eclipse - Android-Studio
- TextEdit Clear Formatting when Pasting Text - MacOS
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED - Java
- [IRCTC] Indian railways official eRail API 1.1 for developers to get train info - HowTos
- How to close tabs on Notepad++ on Click? - NotepadPlusPlus
- SharePoint Online REST API not returning all list items and limit to only 100 rows - SharePoint
- How to find the path of formula Installation by Brew - HowTos