Skip to content

Aliases and native utils name collision #567

Closed

Description

@vors

We have 24 conflicting names between PS aliases and /bin, /usr/bin, etc folders

# /bin
cat              
cp               
echo             
kill             
ls               
mv               
ps               
pwd              
rm               
rmdir            
sleep
# /usr/bin
cd                     
clear                  
cpp                    
diff                   
fc                     
man                    
ri                     
sort                   
tee                    
type                   
write         
# /usr/sbin
ac
# /sbin
mount

We need to figure out what's out story about it.

I think it make sense to play around with different options to see pros and cons on practice.

Try it (the snippet is outdated, see the new snippet for 0.3.0 bellow)

You can add this snippet to your profile and easily switch from one mode to another

  • Remove-PSAliases will remove aliases, so you can use native utilities.
  • Set-PSAliases will restore aliases.
$global:__aliases = @(
    'cat',              
    'cp',               
    'echo',             
    'kill',             
    'ls',               
    'mv',               
    'ps',               
    'pwd',              
    'rm',               
    'rmdir',            
    'sleep',
    'cd',                     
    'clear',                  
    'cpp',                    
    'diff',                   
    'fc',                     
    'man',                    
    'ri',                     
    'sort',                   
    'tee',                    
    'type',                   
    'write',         
    'ac',
    'mount'
) 

function Set-PSAliases
{
    $global:__aliases | % { 
        Write-Host "Adding $_"
        Set-Alias $_ $global:__aliases_backup[$_] -Scope Global 
    }      
}

function Remove-PSAliases
{
    if (-not $global:__aliases_backup) {
        $global:__aliases_backup = @{}
        $global:__aliases | % {
            $global:__aliases_backup[$_] = (Get-Command $_).ResolvedCommandName
        }
    }

    $global:__aliases | % { 
        Write-Host "Removing $_"
        while (Test-Path alias:$_) {
            Remove-Item -Force alias:$_ 
        }
    }
}

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    Usability(LEGACY) Helps filter issues that might be higher priority because they directly affect usability
    on Feb 20, 2016
  2. andyleejordan commented on Feb 21, 2016

    @andyleejordan
    Member

    Is this much of an issue? A user of PowerShell, I think, would expect the cmdlets to be preferred because of their object-orientedness, and the native commands can be easily accessed by absolute path or Get-Command.

  3. vors commented on Feb 21, 2016

    @vors
    CollaboratorAuthor

    Just observed that I did a look-up only in /bin, but not in other folders from PATH.
    The original list of 11 indeed looks fine.

    I'm worry about diff and mount.

    Diff (Compare-Object) behaves very differently from what you would expect (i.e. there is no way to perform a diff in git sense of diff. Lines order is always lost).
    I even wrote a normal diff tool as one of the first things for PS https://github.com/vors/PSCompareCollection

    Mount is tricky for the same reason. It behaves too differently and PS version is not a true replacement.

  4. andyleejordan commented on Feb 22, 2016

    @andyleejordan
    Member

    I agree, diff and mount are worrisome because the Linux behavior is likely to be preferred by default, same goes for man.

  5. zach-folwick commented on Mar 2, 2016

    @zach-folwick
    Contributor

    As a user, I would expect to be forced to either remove or over-ride the default powershell aliases myself, and replace with the linux alternatives, or to use the absolute path of the external command. I don't see how this breaks down with diff and mount.

  6. vors commented on Mar 2, 2016

    @vors
    CollaboratorAuthor

    When I think thru the first time experience for somebody, who never tried powershell, but know bash

    • ok, now I'm here, lets get some stuff done
    • cd, cd, ls, wow the output looks different, I guess I can live with that.
    • ps, kill, yada yada, well this guys did a decent job replicating Unix utilities.
    • diff, wait what? What diff does? You kidding.
    • exit and stay away from it

    In this thought experiment, the concept of aliases vs full command path even doesn't come to play.

  7. andyleejordan commented on Mar 23, 2016

    @andyleejordan
    Member

    From Jeffrey Snover (@jpsnover):

    We need to eliminate all the PS aliases for the Linux cmds
    e.g. ls needs to be the real ls, etc.
    We should probably ship with a sample profile with a commented out section that redefines this for the people that want them.

  8. andyleejordan commented on Apr 2, 2016

    @andyleejordan
    Member

    For the 0.3.0 release, we will remove all the conflicting aliases.

  9. joeyaiello commented on Apr 7, 2016

    @joeyaiello
    Contributor

    We have a naive agreement that most of these need to be removed at build-time on just Linux, with the exception of a few that still need to be defined (e.g. cd).

    That being said, there's some more thinking we have to do. One option is to create a formatter that makes the output of something like Get-Process look like the string output of '/bin/ps', and then enable it being piped to awk/sed. We have to think about it more.

  10. jpsnover commented on Apr 7, 2016

    @jpsnover
    Contributor

    The mindset for this is that it will be used as an interactive shell by Linux users.
    As such we should minimize surprises.
    The last time I looked ps (or ls) had like 80 switches so there is no way we are going to emulate that.

    Sent from my tablet

    On Apr 7, 2016, at 11:48 AM, Joey Aiello notifications@github.com wrote:

    We have a naive agreement that most of these need to be removed at build-time on just Linux, with the exception of a few that still need to be defined (e.g. cd).

    That being said, there's some more thinking we have to do. One option is to create a formatter that makes the output of something like Get-Process look like the string output of '/bin/ps', and then enable it being piped to awk/sed. We have to think about it more.

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub

  11. andyleejordan commented on Apr 8, 2016

    @andyleejordan
    Member

    These are the native command (not Bash built-in) collisions on my Ubuntu 14.04 box.

    I'll compile these out of InitialSessionState.cs. The one thing that I don't like about this approach is that it leaves users without an easy way to put them all back at once (a PowerShell experience).

    But, in my opinion, all of the built-in aliases would be much better off defined in a module outside of the System.Management.Automation library to begin with. Aliases really shouldn't be built into an engine; I don't know of any other shell that does this. Bash has default aliases... defined in /etc/bashrc or your default ~/.bash_aliases etc., which lets users and distributions easily modify them.

    ~/PowerShell |-/ Get-Command -Type Alias | %{ which $_.Name } 
    /bin/cat
    /usr/bin/clear
    /usr/bin/compare
    /bin/cp
    /usr/bin/cpp
    /usr/bin/diff
    /bin/dir
    /bin/echo
    /bin/kill
    /bin/ls
    /usr/bin/man
    /bin/mount
    /bin/mv
    /bin/ps
    /bin/pwd
    /bin/rm
    /bin/rmdir
    /bin/sleep
    /usr/bin/sort
    /sbin/start
    /usr/bin/tee
    /usr/bin/write

    I also had cli which was installed by Mono, and ri which was installed by rbenv. We could remove these, but there will always be some level of collision among tool names.

  12. andyleejordan commented on Apr 8, 2016

    @andyleejordan
    Member

    I had these on my OS X box:

    ~/PowerShell |-/ Get-Command -Type Alias | %{ which $_.Name }
    /usr/sbin/ac
    /bin/cat
    /usr/bin/cd
    /usr/bin/clear
    /bin/cp
    /usr/bin/cpp
    /usr/bin/diff
    /bin/echo
    /usr/bin/fc
    /bin/kill
    /bin/ls
    /usr/bin/man
    /sbin/mount
    /bin/mv
    /bin/ps
    /bin/pwd
    /bin/rm
    /bin/rmdir
    /bin/sleep
    /usr/bin/sort
    /usr/bin/tee
    /usr/bin/type
    /usr/bin/write

    I also had gm which is GraphicsMagick, compare which is from ImageMagick, and rp, which is a "Tool to find ROP sequences in PE/Elf/Mach-O x86/x64 binaries" and I don't know why I had it installed.

  13. andyleejordan commented on Apr 8, 2016

    @andyleejordan
    Member

    We do not currently have a compile time constant for OS X as it shares LINUX since (so far) every compile time difference has been the same. These changes do not necessarily have to be compile-time; just the current way it's written needs to be refactored to be make these runtime changes.

    The only differences between the two are that OS X additionally has the ac command, and (strangely) has native commands for what are only Bash built-ins on Linux: cd, fc, and type.

  14. andyleejordan commented on Apr 8, 2016

    @andyleejordan
    Member

    This brings up an interesting point: Bash itself stomps on quite a few native commands with its own built-ins (which are not aliases, but similar). So perhaps my earlier comment on moving them all out was on an erroneous understanding.

    Bash built-ins stomp on these on Linux:

    ~ |-/ for i in `<linux_commands`; do help $i 2&>/dev/null && echo $i; done
    dir
    echo
    kill
    pwd

    And these on OS X:

    ~ |-/ for i in `<osx_commands`; do help $i 2&>/dev/null && echo $i; done
    cd
    echo
    fc
    kill
    pwd
    type

    I am of the opinion to mirror Bash and let PowerShell stomp on these as well, as that would be what Linux users (possibly unknowingly) expect.

  15. vors commented on Apr 8, 2016

    @vors
    CollaboratorAuthor

    I'm surprised kill is in the list of bash stomped commands. What's the reason for that?
    On the other hand, why cd is not in Linux, but it is in OS X?

  16. andyleejordan commented on Apr 8, 2016

    @andyleejordan
    Member

    My guess is that kill is stomped so that kill in Bash can deal with Bash jobs properly. I am unsure of the history leading to why BSD but not Linux has a native command for cd.

  17. JamesWTruher commented on Apr 8, 2016

    @JamesWTruher
    Collaborator

    removing the alias for cd isn't going to work very well - it's a shell built-in (even if it does exist in the filesystem)

  18. andyleejordan commented on Apr 8, 2016

    @andyleejordan
    Member

    Yes, we should keep the aliases that Bash already had as built-ins, including those that have native executables.

  19. added a commit that references this issue on Apr 8, 2016
    7d9f439
  20. vors commented on Apr 12, 2016

    @vors
    CollaboratorAuthor

    Here is a snippet to resurrect aliases in 0.3.0

    $global:__aliases = @{
    #ri                             = "Remove-Item"      
    diff                           = "Compare-Object"   
    #echo                           = "Write-Output"     
    #kill                           = "Stop-Process"     
    ps                             = "Get-Process"      
    tee                            = "Tee-Object"       
    #type                           = "Get-Content"      
    ac                             = "Add-Content"      
    rm                             = "Remove-Item"      
    write                          = "Write-Output"     
    rmdir                          = "Remove-Item"      
    #clear                          = "Clear-Host"       
    mount                          = "New-PSDrive"
    man                            = "help"             
    #cd                             = "Set-Location"     
    #fc                             = "Format-Custom"    
    cpp                            = "Copy-ItemProperty"
    sort                           = "Sort-Object"      
    cat                            = "Get-Content"      
    cp                             = "Copy-Item"        
    sleep                          = "Start-Sleep"      
    ls                             = "Get-ChildItem"    
    #pwd                            = "Get-Location"     
    mv                             = "Move-Item" 
    }
    
    function Set-PSAliases
    {
        $global:__aliases.Keys | % { 
            Write-Host "Adding $_"
            Set-Alias $_ $global:__aliases[$_] -Scope Global 
        }      
    }
    
    function Remove-PSAliases
    {
        $global:__aliases.Keys | % { 
            Write-Host "Removing $_"
            while (Test-Path alias:$_) {
                Remove-Item -Force alias:$_ 
            }
        }
    }
    
  21. vors commented on Apr 12, 2016

    @vors
    CollaboratorAuthor

    TIL, which is a built-in command in zsh, but not in bash.
    It leads to a weird flow:

    bash-3.2$ which time
    /usr/bin/time
    

    But really time is a built-in keyword.

    And even worse, it has a different parameters from an util one.

    /usr/bin/time -l sleep 10
    

    Is the same as

    time sleep 10
    

    And note, there is no -l.

    http://stackoverflow.com/a/19237170/1355726

  22. added a commit that references this issue on Aug 6, 2026
    699eedc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aUsability(LEGACY) Helps filter issues that might be higher priority because they directly affect usability

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions