How create alias for a path in bash .profile?

1k Views Asked by At

Under macOS, in .profile I want to create an alias myproj for a path, say ~/Documents/mywork/project, so that in Terminal I can use the command:

open myproj

I tried simply adding to .profile the line...

alias myproj="~/Documents/mywork/project"

... but after restarting Terminal, when I then try open myproj I get error: The file /Users/me/myproj does not exist.

What's wrong?

1

There are 1 best solutions below

0
On

alias doesn't work that way.

Try

alias myproj="open ~/Documents/mywork/project"

You type

myproj # NOT "open myproj" 

the parser processes the alias and changes it to

open ~/Documents/mywork/project

Addendum

Maybe an alias isn't really what you wanted anyway.

You could do it more like your original attempt with a variable.
Instead of

alias myproj="~/Documents/mywork/project"

try just

myproj="~/Documents/mywork/project"

Then to open it,

open $myproj # breaks if there are embedded spaces...

or, better,

open "$myproj"

If you want more functionality, consider a function instead, though for this it's probably not needed.