Bash script command result inside other variable to define prompt

0 votes

I would like to define a prompt which will indicate with colors whether the command executed properly and whether the command was found. As for now I have something like this but I does not work properly.

PS1="\`COMMAND_RESULT=\$\?;

if [ $COMMAND_RESULT -eq 127 ]; then echo \[\e[33m\] ---=== Command not found ===--- ;  

elif [ $COMMAND_RESULT -ne 0 ]; then echo \[\e[33m\]---=== \[\e[31m\]Oh noes, bad command \[\e[33m\]==---; 

fi\`

\n\[\e[0;37m\][\[\e[1;31m\]\@\[\e[0;37m\]] 

\[\e[0;32m\]\u\[\033[1;33m\]@\[\033[0;32m\]\h 

As for now I get this error on bash start :

-bash: [: -eq: unary operator expected
-bash: [: -ne: unary operator expected
Jun 22, 2022 in Linux Administration by Korak
• 5,820 points
245 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Linux Administration

0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31, 2019 in Linux Administration by Omkar
• 69,210 points
857 views
0 votes
1 answer

setting a variable output from a bash command

You can use $(command), which in my ...READ MORE

answered Feb 15, 2019 in Linux Administration by DareDev
• 6,890 points
2,190 views
0 votes
1 answer

How to run one command inside another command in Linux?

Hi@akhtar, You can run multiple commands in your ...READ MORE

answered Oct 15, 2020 in Linux Administration by MD
• 95,440 points
2,586 views
0 votes
0 answers

Send private message to Discord user using bash and linux AT command

I'm running a Discord bot written in ...READ MORE

Apr 25, 2022 in Linux Administration by Edureka
• 13,670 points
938 views
0 votes
0 answers

Bash script to automate a VPN from vpnbook.com

I wrote a bash script that reads ...READ MORE

May 27, 2022 in Linux Administration by Rahul
• 3,380 points
1,432 views
0 votes
1 answer

Is there a better way to run a command N times in bash?

In the event that your reach has ...READ MORE

answered Jun 13, 2022 in Linux Administration by Korak
• 5,820 points
412 views
0 votes
1 answer

Is there a better way to run a command N times in bash?

In the event that your reach has ...READ MORE

answered Jun 16, 2022 in Linux Administration by Korak
• 5,820 points
299 views
0 votes
0 answers

How to send control+c from a bash script?

I'm starting a number of screens in ...READ MORE

Jun 16, 2022 in Linux Administration by Rahul
• 3,380 points
601 views
0 votes
1 answer

Why do you need to put #!/bin/bash at the beginning of a script file?

It's a show so the *nix shell ...READ MORE

answered Jun 20, 2022 in Linux Administration by Rahul
• 3,380 points
705 views
0 votes
0 answers

How to detect the OS from a Bash script?

I would like to keep my .bashrc and .bash_login files in ...READ MORE

Jun 21, 2022 in Linux Administration by Korak
• 5,820 points
371 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP