Where should I define shell aliases? That is, what is the best practice (and why)?

I can think of the following options:

  1. .bashrc
  2. .cshrc
  3. .tcsh
  4. ...
  • 10
    DEAR CLOSE VOTERS: There's 3 votes on this question: opinion based, too broad, and unclear what you're asking. IMHO , these 3 votes are not valid at all - there's a clear , very specific question and answers to this question will not be based on opinion. Please retract your votes Oct 31 '16 at 11:26
  • 1
    @Serg I find it very unclear: does OP know what theyre talking about at all? Besides, even for a single shell, best practice is a matter of opinion - whether you use bashrc or another file being sourced in bashrc, for example. Your own answer makes no mention of "best practice" asked in the question.
    – muru
    Oct 31 '16 at 12:18
  • 2
    @muru "knowing what they're talking about" is not a prerequisite to asking questions on the site; if everybody knew what they're talking about, there would be no need for AskUbuntu in the first place. Second, there's no best way , there's standard ways of placing aliases into your shell's rc file. Bash man page doesn't mention ~/.bash_aliases so that's not standard - this is just a clever workaround. Finally, look at "opinion based" description - it says answers to this question will tend to be almost entirely based on opinions . . . so it involves answers more so than question itself Oct 31 '16 at 12:37
  • @Serg but if you have no idea of how much of the subject matter the OP understands, how can you write an answer you know they'll understand? And if you can't write such an answer, what's the point of this site? And the question is "best practice", not standard, which goes above and beyond the manual. (Also, "clever workaround"? Workaround for what? Isn't that an opinion, and isn't that an opinion on something mentioned in an answer here?)
    – muru
    Oct 31 '16 at 12:40
  • 1
    @Serg anyway, you can continue arguing. I have no intention of retracting my close vote until I see improvement in the question from the OP. The very fact that three people chose three different close reasons says much about the clarity of the question.
    – muru
    Oct 31 '16 at 12:41

The options you mention refer to different shells. When a user starts a shell program, the relevant rc file in the user's home directory is read. If you are using bash (as you will be when you open a terminal in Ubuntu, unless you have installed and selected a different shell as default for your user), then you should put your aliases either in .bashrc, or if you prefer (for example so you can more easily see and edit all your aliases), in a dedicated .bash_aliases file. This file does not exist by default, but if it is created, it will be sourced when bash is started by the default .bashrc:

$ grep -n '^[^#]*bash_alias' /etc/skel/.bashrc
104:if [ -f ~/.bash_aliases ]; then
105:    . ~/.bash_aliases

If you are using another shell, then use the appropriate rc for that shell.

  • Will .bash_aliases be sourced automatically without when created without putting anything in .bashrc?
    – Anwar
    Oct 31 '16 at 13:43
  • 1
    On Ubuntu, the default ~/.bashrc contains several lines that source ~/.bash_aliases if it exists. Do for example, grep -n -i 'bash_alias' /usr/share/base-files/dot.bashrc Oct 31 '16 at 16:17
  • @Serg That was I need. Thanks. It's Ubuntu modification
    – Anwar
    Nov 1 '16 at 6:49
  • 1
    @Anwar edited it to show the lines in default .bashrc that source .bash_aliases if it exists
    – Zanna
    Nov 1 '16 at 6:57

What shell you're using, determines which file to use. If you use bash, it makes no sense to define them in .cshrc simply because bash won't read it, and vice versa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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