Bash Alias Function Same name Simultaneous definition

Be Cautious About Defining Same Names for Aliases and Functions in Bash

A story about being careful with simultaneously defining aliases and functions with the same name in the Bash programming language (scripting language) command line on Linux PCs/servers. Unexpected 'syntax error near unexpected token' errors are often caused by this. Even though spell check shows no grammar issues, when that happens...

Shou Arisaka
2 min read
Oct 14, 2025

A story about being careful with simultaneously defining aliases and functions with the same name in the Bash programming language (scripting language) command line on Linux PCs/servers.

Unexpected syntax error near unexpected token errors are often caused by this. When you paste your usually used .bashrc to another remote environment’s .bashrc and reload, you may get this error. Hmm, it doesn’t error in my local environment, and spell check shows no grammar issues. Why? In such cases, it’s better to suspect this cause before checking the bash version.

Suppose you have defined an alias and function with the same name as follows:

something(){
  echo something
}

alias something="echo something"

Let’s try defining these in two different orders. Here are the results:


yuis@yuis:~$ bash
yuis@yuis:~$
yuis@yuis:~$ something(){
>   echo something
> }
yuis@yuis:~$
yuis@yuis:~$ alias something="echo something"
yuis@yuis:~$
yuis@yuis:~$ bash
yuis@yuis:~$ alias something="echo something"
yuis@yuis:~$ something(){
bash: syntax error near unexpected token `('
yuis@yuis:~$   echo something
something
yuis@yuis:~$ }
bash: syntax error near unexpected token `}'
yuis@yuis:~$
yuis@yuis:~$

Image

The former first defines the function, then defines the alias. There’s no error, which seems normal. The latter has the order reversed, but an error occurred.

Functions are weaker entities than aliases, so while you can overwrite something defined as a function with an alias, you cannot overwrite something defined as an alias with a function.

Since one of them produces no error, this problem is hard to notice.

The same thing happens when you do this in .bashrc. Or, if you define an alias in bashrc and try to define a function with the same name on the console, it will error. Even if you update bashrc to eliminate such things, if you don’t reload bash, the previously defined alias remains, so loading the function in bashrc or on the console will error.

Share this article

Shou Arisaka Oct 14, 2025

🔗 Copy Links