Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

My problem is: whenever I'm coding, I work inside folders that are inside other folders and it's a long way to type:

cd /folder1/folder2/.../folderN

I know it's possible to make a shortcut to the folder using alias. So it would be:

alias ep="cd /Users/thi/Dropbox/CurrentProject"

My question is, where should I put this alias? Inside /etc/bashrc? Or should I create a file called ~/.profile and place it there? Where should I put it?

share|improve this question
    
I think what would take you to the next level is learning about shell completion. bash's completion is pretty basic, but zsh has extensible completion. For example ls -l P/H/O/Cla<tab> expands to ls -l Projects/Home/OpenGL/Clams –  Mark Aug 23 '13 at 18:47

3 Answers 3

up vote 1 down vote accepted

I keep aliases in ~/.bash_profile.

Terminal and iTerm 2 open new shells as login shells by default. When bash is invoked as an interactive login shell, it reads ~/.bash_profile but not ~/.bashrc. The terminal emulators on other platforms often open new shells as non-login shells, so for example bash reads ~/.bashrc but not ~/.bash_profile. OS X users often use ~/.bash_profile as the personal configuration file corresponding to ~/.bashrc on other platforms, but it is also possible to source ~/.bashrc from ~/.bash_profile or to tell Terminal or iTerm 2 to open new shells as non-login shells.

If both ~/.profile and ~/.bash_profile exist, bash only reads ~/.bash_profile when it is invoked as an interactive login shell. ~/.profile is also used by other shells and programs that might not understand the same configuration options as bash.

/etc/bashrc is owned by root, and it might get replaced when you upgrade OS X.

See man bash|less +^INVOCATION or https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html for more information.

share|improve this answer

Typically you put alias definitions into the same file as your $PATH definitions which is probably ~/.bashrc.


Technically speaking an alias is not a shortcut to a folder but for any shell command. So you can also have things like

alias heal='xattr -d com.apple.quarantine'
alias la='ls -lFa'
alias ll='ls -lF'
alias ls='ls -FG'
alias show-path='echo -e ${PATH//:/\\n}'

to make your life easier. Aliases are replaced as-is by the shell so if you run

heal downloadedFile.dmg

it gets expanded to

xattr -d com.apple.quarantine downloadedFile.dmg

and executed afterwards. If you need more flexibility with parameters you may want to look into shell functions (but that probably should go into another question).

share|improve this answer

i am not expert in bash but i think you should put your alias inside .bashrc in your home directory.

share|improve this answer
    
That is what Patrix said. ~ is a shell shortcut for $HOME. –  Mark Aug 23 '13 at 18:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.