Using aliases in Terminal

Terminal can use aliases for frequently used commands. For example, I have this in my .profile: alias dl='cd Downloads/' to quickly change to my Download folder.


I'm wondering how I can alias the path to a folder and make it usable inside of a shell command. I've added alias dt='Desktop/' to my profile, but when I try to do a command such as "ls dt" (which should list the contents of my Desktop folder), it fails. Any advice on making this work?

15" i5 MacBook 2.4GHz, Mac OS X (10.6.5)

Posted on May 8, 2011 2:50 PM

Reply
18 replies

May 8, 2011 4:06 PM in response to brian_c

From "man bash":


... The first word of each simple command, if unquoted, is checked to see if it has an alias. ...


Basically, an alias is only effective a the first word of a command. It can not be an argument within a command.


HOWEVER, you can create shell variables to hold information and then substitute the shell variable when desired


dt="$HOME/Desktop/"


Now when you want to substitute Desktop/ into a command you just enter


someCommand $dt


This could apply to any bit of text you want to substitute into a command line, it does not need to be a file path.


4 minutes late. I was beaten by Frank Caggiano


Message was edited by: BobHarris

May 11, 2011 5:33 AM in response to BobHarris

Thanks for the replies. I edited my environment.plist to add the Desktop variable without issue. However, I'm having trouble setting up a variable for one particular folder; I think that it's because it has a space in its name. The folder, in plain English, is "/Volumes/iTunes - iMac/Plex Movies"


I added "/Volumes/iTunes\ -\ iMac/Plex\ Movies/" (with the quotes), set to the variable movies. When I run ls $movies I get this error:


ls: "/Volumes/iTunes\: No such file or directory

ls: -\: No such file or directory

ls: Movies/": No such file or directory

ls: iMac/Plex\: No such file or directory


I even tried doing ls "$movies" (notice the quotes around the variable). That gives me the same error, but all printed on one line, like this: ls: "/Volumes/iTunes\ -\ iMac/Plex\ Movies/": No such file or directory


Any suggestions?

May 11, 2011 6:34 AM in response to BobHarris

Thanks again, Bob & Frank... you guys seem to be logged on at the same time!


I can get the variable working properly when I use export movies="/Volumes/iTunes - iMac/Plex Movies/", as you'd each suggested. Of course, the variable won't persist once I log out. So, I'm trying to add it to my environment.plist and make it permanent. That's where I'm having trouble.


Here's what my environment.plist looks like:


User uploaded file


The $dt variable works fine, just not the $movies one. Any further suggestions are more than welcome!

May 11, 2011 7:23 AM in response to brian_c

I think we're over complicating it. This worked for me,


In the plist file put in the pate as is.


/Volumes/iTunes - iMac/Plex Movies/


When you use it put quotes around it:


ls "$movies"


I believe this is the best you're going to get. If the quotes around the variable when you use it are no good you'll have to do like Bob said and not put spaces in paths that you intend to use from the shell.

May 11, 2011 7:27 AM in response to brian_c

Have you considered using $HOME/.bash_profile for your environment variables? It is what all the "Cool" Unix kids are doing. OK maybe it is the "Old" guys :-)


.bash_profile is generally speaking portable to other Unix platforms (assuming you use bash as your shell on those platforms).


It does not require a special utility to edit it.


And it is invoked every time you startup a new Terminal session.


The behavior may not chance much from environment.plist.


See "man bash" for more information about bash shell initialization files.


Message was edited by: BobHarris

May 11, 2011 9:46 AM in response to brian_c

> Ah, you want extra credit? How 'bout this....

>

> I have two Macs... let's call 'em Mac A and Mac B.

>

> I got the variable working as desired on Mac B. It works perfectly when I'm sitting in front of Mac B.

>

> But, when I SSH into Mac B from Mac A, the variable doesn't work. I'm getting: ls: fts_open: No such file or directory

>

> What now? :-/


Because environment.plist is ONLY used by your Mac OS X GUI login. Ssh is NOT a GUI login, so your environment does not exist.


I go back to my "Cool" kids are using .bash_profile. This is invoked by your shell when it starts up, no matter whether it is a new Terminal session or an ssh session.


.bash_profile is portable across multiple Unix platforms.


See "man bash" and search for ".bash_profile" for more information about shell initialization files.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using aliases in Terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.