16

I have this as the last line in my .profile:

alias gl="cd /home/jrenner/glances/glances"

yet even after reboot I get command not found when typing gl. What is happening? I am logged in as the correct user.

  • Where are you typing gl? The terminal? The GUI? – Thomas Ward Mar 26 '13 at 02:58
  • 1
    Nobody ever addressed the original why? question. I can confirm that aliases defined in .profile do not work in any context I tried: text console logins, graphical terminals, or anything else, even though enviromental variables from the same file do work. – Daniel Mahler Apr 06 '16 at 05:57
  • @DanielMahler, do you have a reason why? – Maged Saeed Mar 03 '20 at 08:24

5 Answers5

13

There are two related reasons why aliases don't always work when put in the .profile file. The first is that the .profile (or .bash_profile) file is only run for a login shell. If you are starting bash in a terminal window under X, your terminal emulator (e.g. gnome-termanl) probably isn't running bash as a login shell. [Most have an option to change this if you want but the default (for gnome-termal anyway) is not to run it as a login shell.]
The shell will be an interactive shell and so .bashrc will be run.

However, normally bash has been run as a login shell back when the X session was being started. So if there are alias commands in .profile they will have been executed along with setting environment variables like the PATH etc. When a terminal window is opened a new instance of bash is run to prompt for, and execute commands in that terminal window. Unlike environment variables, aliases can not be exported from one instance of bash to a new one started by it. So the aliases are not passed on to the new shell.

To see this, try this experiment:

export ROBERT=bob
alias james=jimmy
echo $ROBERT
alias james
bash               #start a new bash instance
echo $ROBERT
alias james
exit               #end the new bash instance and revert to the original one
echo $ROBERT
alias james

Note that .bashrc is not run by bash when it is started as a login shell. So putting your aliases there won't always work unless your .bashrc is sourced from your .profile, which is a very common practice.

Zanna
  • 70,465
  • Verified: adding source .bashrc in .bash_profile allowed the alias set in .bashrc to work. – Treefish Zhang Sep 03 '17 at 14:00
  • Looks like this should be the correct answer. Also, in Ubuntu just add your aliases in ~/.bash_aliases. That's the right way to do it. (read the .bashrc file for details). – Emilio Apr 14 '20 at 15:07
4

The right way to do this in Ubuntu is to add your alias to ~/.bash_aliases. Create the file if it doesn't exist.

This file (if present) is called from the default ~/.bashrc, and the alias will be available in your terminal emulators too.

Emilio
  • 321
4

I'm pretty sure that lpanebr's idea will work, but here's a more elegant solution. Do that alias command in .bashrc That's how I do it, or some people prefer to add a file dedicated to alias. Call it .alias or whatever and add .alias to your .bashrc

Wish I could do formatting like @lpanelbr. I wonder if there is a wiki?

Jason
  • 583
  • 1
    As for the formatting askubuntu uses markdown. Please check out http://askubuntu.com/editing-help for the details. – saji89 Mar 26 '13 at 04:23
  • 1
    Why does .bashrc work while .profile doesn't? Also, any reason why you chose to name the alias file .allias rather than .alias? – Tom Apr 04 '15 at 22:59
  • #1 From what I understand .bashrc is specific to bash and .profile is specific to non-bash command line. #2 I don't know what you mean. I can spell after all. (edited) – Jason Sep 24 '17 at 23:13
0
$ bash #start a new bash instance

This was problem with my server. Simple source .profile didn't work for some reason

0

If you are using bash (echo $SHELL) as a shell interpreter I think it is best (or maybe the only way) if you define your own CD function.

Edit your .bashrc file and add the following at the end:

# my custom functions:
function gl () {
   cd /home/jrenner/glances/glances/
}

You could also extend it like so:

# my custom functions:
function cdw () {
   cd /home/jrenner/glances/glances/$1
   ls
}

and change directly to a sub directory and list it's contents.

lpanebr
  • 1,277
  • 2
    That may be a fine suggestion, but it is not an answer, as it doesn't explain why the alias directive placed in the .profile file fails. – Tom Apr 04 '15 at 22:58