4

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?

  • 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 – Ɱark Ƭ Aug 23 '13 at 18:47
4

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.

1

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).

1

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

  • That is what Patrix said. ~ is a shell shortcut for $HOME. – Ɱark Ƭ Aug 23 '13 at 18:43
1

This answer neatly summarises what ideally belongs in each of ~/.bashrc, ~/.profile, and ~/.bash_profile. In summary:

  • ~/.bash_profile should be super-simple and just load .profile and .bashrc (in that order)

  • ~/.profile has the stuff NOT specifically related to bash, such as environment variables (PATH and friends)

  • ~/.bashrc has anything you'd want at an interactive command line. Command prompt, EDITOR variable, bash aliases for my use

    A few other notes:

  • Anything that should be available to graphical applications OR to sh (or bash invoked as sh) MUST be in ~/.profile

  • ~/.bashrc must not output anything

  • Anything that should be available only to login shells should go in ~/.profile

  • Ensure that ~/.bash_login does not exist.

1

The short answer is: (generally)

Add it to ~/.bash_profile

echo 'alias ep="cd $HOME/Dropbox/CurrentProject"' >> ~/.bash_profile

NOTE: There shouldn't be any space between the alias/variable and the equals sign ("=") and the value. If your value has spaces then the whole value should be put in quotes.

A better answer is: (probably)

Try setting an environment variable, instead. This is probably more like what you actually want.

echo 'export EP="$HOME/Dropbox/CurrentProject"' >> ~/.bash_profile
echo 'alias ep="cd $EP"' >> ~/.bash_profile

NOTE: That environment variables are accessed by prepending the dollar sign ("$"). However, when being defined, the dollar sign ("$") should be omitted.

An alternative answer is: (possibly)

Symbolic-Links. AKA. Symlinks. {FKA. Aliases. (Mac OS)}. {BKA. Shortcuts. (Windows OS)}.

ln -s <dirA/subB/fileC_in> <dirX/subY/symbZ_out>

NOTE: symlinks are sometimes denoted by prepending the ampersand sign ("@").

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .