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!
More Posts related to Bash,- Bash Hello World! Script Tutorial
- How to run bash command in background
- How to create new user account in Windows bash
- How to Compare Strings in Bash
- Bash: Allow Command to Fail without exiting Script
- Fix bash: script.sh: Permission denied Error
- Bash getopts Command Example
- How to fix bash ping command not found error
- How to add NewLine Character in Bash Script String
- How to Echo Bash Command to a File
- Bash Command to Find String in a File
- How to open new Terminal using Bash Command
- Bash How to Save Output of a Command to a Variable
- Download a SSL Certificate from a URL in Terminal
- Bash Command to Check IP Address
- Know Bash shell version command
- Bash Command To Check If File Exists
- How to run a Command in Bash Script
- How to Compare two Files in Bash Shell
- How to check if a variable is set in Bash Script or Not
- Convert String from uppercase to lowercase in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Bash Command To Get Current Time
- Bash command to List Files
- Fix: bash: syntax error near unexpected token
More Posts: - Error: Can not find the tag library descriptor for - Java
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations - Android
- How to always show Bookmarks bar on Google Chrome Browser - Chrome
- Python Program To Calculate Simple Interest (SimpleInterest.py) - Python
- [Fix] MySQL Docker ERROR 1045 (28000): Access denied for user root@localhost (using password: YES/NO) - MySQL
- Base 64 Index, Character and Binary Table - Html
- Convert Java List to Json String using Jackson - Java
- Python copy file from a source to destination - Python
bash-3.2$ cstatus whoami
c2ctech
Command succeeded
bash-3.2$ cstatus whoamaa
bash: whoamaa: command not found
Command failed

- Bash Hello World! Script Tutorial
- How to run bash command in background
- How to create new user account in Windows bash
- How to Compare Strings in Bash
- Bash: Allow Command to Fail without exiting Script
- Fix bash: script.sh: Permission denied Error
- Bash getopts Command Example
- How to fix bash ping command not found error
- How to add NewLine Character in Bash Script String
- How to Echo Bash Command to a File
- Bash Command to Find String in a File
- How to open new Terminal using Bash Command
- Bash How to Save Output of a Command to a Variable
- Download a SSL Certificate from a URL in Terminal
- Bash Command to Check IP Address
- Know Bash shell version command
- Bash Command To Check If File Exists
- How to run a Command in Bash Script
- How to Compare two Files in Bash Shell
- How to check if a variable is set in Bash Script or Not
- Convert String from uppercase to lowercase in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Bash Command To Get Current Time
- Bash command to List Files
- Fix: bash: syntax error near unexpected token
- Error: Can not find the tag library descriptor for - Java
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations - Android
- How to always show Bookmarks bar on Google Chrome Browser - Chrome
- Python Program To Calculate Simple Interest (SimpleInterest.py) - Python
- [Fix] MySQL Docker ERROR 1045 (28000): Access denied for user root@localhost (using password: YES/NO) - MySQL
- Base 64 Index, Character and Binary Table - Html
- Convert Java List to Json String using Jackson - Java
- Python copy file from a source to destination - Python