This is probably a very simple thing but I can’t find an answer, possibly because I don’t know what terms to use in search.

How do I use an alias of a path with mv or cp? Or even cd?

In /etc/bash.bashrc I have: alias docs=‘/media/docs

cd docs Gives “No such file or directory”

Yet: docs Gives “Is directory”

With alias docs=‘cd /media/docs’ and by typing docs I get into the directory. Obviously I can’t use that alias with mv or cp though.

Maybe this isn’t even an intended use of alias but still. Why doesn’t it work?

  • brandon@piefed.social
    link
    fedilink
    English
    arrow-up
    5
    ·
    edit-2
    13 hours ago

    By default bash will only expand an alias if it’s the first argument of the command (that is, the command itself).

    It’s probably not intended to use aliases this way, and there are probably better options for you.

    However, there is a little trick you can do. If the alias command ends in a space, then bash will also check the next argument:

    alias cd='cd '

    Notice the trailing space after ‘cd’ in the alias definition.

    alias docs='/media/docs'

    then, cd docs should work the way you expect.

    Another method would be to:

    alias docs='echo /media/docs'

    Then you can do

    cd `docs`  
    

    The backticks will cause the shell to replace that portion with the output of the docs shell command, which will be expanded via the alias.

    All that said, it’s probably easiest just to use a link, like another commenter suggested.