Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'd like to change the foreground color based on which program is running. Is this possible in either Bash or PowerShell?

share|improve this question

2 Answers 2

In Powershell you can use the Write-Host cmdlet which has the -ForeGround parameter to control the foreground for the text. Be aware though that text written to the console via this cmdlet does not go to the pipeline which can lead to some unexpected behavior if you expect to work with the text later in the pipe.

Here is an example:

(get-help write-host).Description.Text | write-host -ForegroundColor Cyan
share|improve this answer

In bash you can define a bunch of functions:

function command_name
{
    trap ':' 2
    command to set desired color(s)
    full_path_to_command "$@"
    command to restore default color(s)
}

for each command/program you want to have in a different color.  For example,

function date
{
    trap ':' 2
    echo "\033[31m\c"
    /bin/date "$@"
    echo "\033[30m\c"
}

The trap command causes the function to keep on running if you interrupt the program (by typing Ctrl+C).  Without it, if you type Ctrl+C while your colored program is running, your terminal will be left in the color for that command.  (There may be better ways to handle this.)

"$@" is replaced by the arguments you type to the function, so if you say date -u, it knows to pass the -u option to the /bin/date command.

You might want to define a function that does nothing but restore your default color(s).  Since that doesn't need to take arguments, it can be done as an alias.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.