Benjamin
Benjamin

Reputation: 280

Bash alias causing error "bash: cd: too many arguments" only when set in .bashrc

I have several bash aliases set in my .bashrc but they generate errors every time I open a new terminal. Each time a new terminal opens, two bash: cd: too many arguments will appear. The aliases work as intended, but I would like to solve the errors anyway. Here are the aliases in question:

alias .1="cd .."
alias .2="cd ../.."
alias .3="cd ../../.."
alias .4="cd ../../../.."
alias .5="cd ../../../../.."

alias .=".1"       #Trouble maker
alias ..=".2"
alias ...=".3"
alias ....=".4"
alias .....=".5"

I have narrowed it down to alias .=".1" as the culprit creating the errors. I understand the . is its own command and I am slapping an alias on top of it. I am not sure this is the issue or not, but I have noticed when I remove this line the errors disappear. Furthermore, running the alias on the CLI itself does not generate the same errors... only when in the .bashrc does it generate the errors.

Things I have tried:

  1. Quotations on both sides of =
  2. Changing alias .=".1" to alias .="cd .."
  3. Adding a space before alias command in an attempt to suppress output

Upvotes: 2

Views: 1161

Answers (2)

sf_admin
sf_admin

Reputation: 577

The alias that is causing the error is: alias .=".1". The single period is a synonym for the source command, which reads in and executes commands from the file you pass as its argument.

What you're essentially doing (unintentionally), is trying to change the behavior of the source command using an alias.

Upvotes: 1

chicks
chicks

Reputation: 2463

Aliasing . means you are changing how subsequent shell commands include other shell scripts. Since . and source do the same thing in bash you might be able to fix this by making sure that they're only using source. Look in .bash_profile for instance. Bash looks at a variety of files while it is starting and .bashrc is probably getting read by .bash_profile and something in there is trying to . some other file.

But really, why? Can you just add a dot to each of these and be ok with it? The .. directory is one level up so that could make it easier to remember. Changing things like this that are fundamental to how the shell operates and glues together multiple scripts is going to keep tripping you up.

Upvotes: 2

Related Questions