Elijah Lynn
Elijah Lynn

Reputation: 13468

alias .="cd .." causing bash to start at root

If I add the alias alias .="cd .." to my .bash_aliases file (sourced from .bashrc) it causes every new shell I make to start at /. I am guessing this is just a fundamental thing I don't understand.

Any ideas?

Upvotes: 1

Views: 1522

Answers (2)

Gordon Davisson
Gordon Davisson

Reputation: 125708

. is a builtin shell command, equivalent to "source" -- it executes a shell script in the current shell, thus allowing it to define variables, functions, etc.

This . command is used fairly often in scripts to bring in definitions from other scripts. When you alias . to something else, you override the standard definition and break every script that uses it.

Please do not redefine standard commands.

(P.S. it's actually more complicated than that, because aliases are only active in interactive shells, so . would do completely different things in interactive vs. non-interactive shells. This doesn't really help the situation.)

Upvotes: 2

fedorqui
fedorqui

Reputation: 289505

. refers to the current directory, while .. refers to the one upper in the dirs hierarchy.

What must be happening is that in your bashrc you have some . after this alias definition, so it gets called and hence you are moved to the parent directory.

So:

  • you enter and you are in /home/your_home
  • you set alias .="cd ..".
  • some dot . is found while reading .bashrc.
  • this dot is executed as to be an alias, so you get cd .., which moves you to /home.
  • you probable have more than one . and it must get executed again, moving you from /home to /.

Solution:

  • Create an alias with a better name that does not have any predefined meaning.
  • Move your alias sourcing to the bottom of .bashrc.

Upvotes: 1

Related Questions