Categories
Software Development

Why doesn’t bash script recognize aliases

Here I won’t be doing much explaining, just listing links so people can read about this befuddling issue.

It often boggles my mind how differently you must write shell scripts vs the command line commands. It is often very inconsistent, I hate inconsistencies.

Basically Aliases within Linux shell are not recognized without some fancy hacky code, WOOHOO. So you will lose your mind if you are trying to set and use aliases in shell scripts.

You can set aliases all day long, but your scripts wont use them.

Yes you read that correctly. You can set aliases in your script, even right before you want to use it and Linux is like GTFO, I have no idea what that is. It even fails without a notice/error most times. What you can do is set a normal variable and use it in place of an alias though.

Say you had a script named do-this-thing.sh  and it was located deep in a directory like /etc/directory/directory/directory/directory/do-echo “$yarnBin” > /etc/profile.d/server-alias.shthis-thing.sh
You could do the following in BASH


not_alias=/etc/directory/directory/directory/directory/do-this-thing.shalias not working inside bash shell script

bash not_alias

The above would execute the do-this-thing.sh file. You can also permanently set aliases in your Shell script. This is handy even if you can’t use the aliases in your script directly, you can use them in the terminal command line later.

To permanently set aliases alias when not working inside bash shell script place them in your .bashrc file for the user you are logged in   This is usually located in /home/username/.bashrc  or you can put it in the user profile .profile file or other places.
How you do this varies by Linux shell.

Links to more info

Why doesn’t my Bash script recognize aliases?

Alias not working inside bash shell script

Creating permanent executable aliases

How to create permanent Linux Aliases

How to create a permanent Bash alias on Linux/Unix