3

I'm currently running Gentoo Linux and have installed PowerShell (app-shells/pwsh-bin) as I am a contrarian.

That being said, I am unable to obtain colour by default when running certain commands. Take ls or grep. When running ls or grep in a normal shell (e.g. bash) I will get colourful output by default. When running ls or grep in PowerShell, I must manually call these two commands with ls --color or grep --color to obtain colour. That being said, commands such as emerge do have colour by default in PowerShell (though emerge doesn't work, which is another issue).

I was wondering what I have to do to ensure that these commands have colour. Perhaps PowerShell doesn't use the default utilities and defines ls as an alias for its own command. I don't know how to use this information to solve my problem, though.

edit: The provided answer does not really solve my problem. While I now have colour when running ls, I do not have colour when running grep. Additionally, I get plenty of errors with the provided profile along the lines of:

Set-Alias: /home/jj/.config/powershell/Microsoft.PowerShell_profile.ps1:2 Line | 2 | set-alias cd set-location | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The AllScope option cannot be removed from the alias 'cd'.

2
  • You might be interested in reading : wiki.installgentoo.com/wiki/Powershell
    – MC68020
    Commented Feb 3, 2023 at 16:37
  • @MC68020 "Install Gentoo" is a meme in the 4chan community, which is why the wiki is named as such. The page you're linking isn't actually about installing PowerShell on Gentoo and hasn't been updated for PowerShell on Linux.
    – JJ Marr
    Commented Feb 6, 2023 at 17:53

1 Answer 1

3

The ls and grep PowerShell uses are the same binaries other shells use. In my Bunsenlabs Berylllium (Deb 11) these are /usr/bin/ls and /usr/bin/grep. Colorization is defined with aliases in .bashrc.

alias grep='grep --color=auto'
alias ls='ls --color=auto'

Aliases defined in .bashrc etc. don't pass on to PS -session. However PS has $profile -files (see Microsoft) "to customize your environment and to add session-specific elements". To find the profile just type $profile in the shell. In my system the default is ~/.config/powershell/Microsoft.PowerShell_profile.ps1, but this file didn't exist after installation. The MS link has instructions how to create one, which I didn't follow - I simply created the directory and wrote the file.

After researching the subject looks like the easiest way is aliasing the commands in $profile, combined with defining functions. I ended up with this as my $profile:

if ($host.Name -eq 'ConsoleHost')
{
    function ls_col { & '/usr/bin/ls' --color=always $args }
    Set-Alias -name ls -Value ls_col -Option AllScope
    function grep_col { & '/usr/bin/grep' --color=always $args }
    Set-Alias -name grep -Value grep_col -Option Allscope
}

... and ls and grep are colored the same way as in my Bash-sessions.

Allscope-option is explained here.

Note - this is just quick n' dirty testing, not thorough and exhaustive how-to. The links should help to take this further. And more knowledgeable people are likely to correct me :-)

4
  • This doesn't actually fix my issue as grep does not have colour. The provided example profile also gives me a ton of warnings along the lines of the following: Set-Alias: /home/jj/.config/powershell/Microsoft.PowerShell_profile.ps1:2 Line | 2 | set-alias cd set-location | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The AllScope option cannot be removed from the alias 'cd'.
    – JJ Marr
    Commented Feb 6, 2023 at 17:56
  • @JJMarr - to satisfy my curiosity, did the revised answer help you? Commented Feb 17, 2023 at 10:40
  • I discovered that ls and grep were in /bin rather than /usr/bin on my installation, so this answer needed a little tweaking. However, using the provided $profile file with /bin/ls and /bin/grep works for me.
    – JJ Marr
    Commented Feb 27, 2023 at 15:49
  • That's great to know, thanks for letting me know. As you might've guessed from me re-writing the whole answer I actually didn't know until your question prompted me to research the subject so I learned something new and useful :-) Commented Feb 27, 2023 at 16:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .