macosbashterminalalias.profile

Terminal (Mac) create alias that includes user input


In Mac OS Terminal, I'm learning the basics of Unix. I'm having trouble with a hopeful easy fix, but cannot figure out where to start looking.

cd __________ && ls

That is a pretty common pattern for me, to check and see the file folder I'm working in. To save myself keystrokes, I thought to make an alias in my .profile

alias cd='cd && ls'

Now, the obvious flaw (which was not obvious to me) was I would not be able to give it a directory to actually change to

cd ~/Documents/ && ls

This is what I would like to do, but not have to type those last four characters. Any ideas on how I could incorporate my user input (maybe some kind of $(pbpaste) option?

Any help or suggestions would be appreciated.


Solution

  • You need to first create BASH function for this instead of alias:

    mycd() { cd "$1"; ls; }
    alias cd='mycd '
    

    then use it like this:

    cd ~/Documents/