First, aliases are temporary and valid only for the currently running shell. When you run the alias.sh
file, it executes in its own shell, not in the shell it was run from. So the script sources ~/.bash_aliases
(why this file and not /etc/.bash_aliases
? I thought you want the latter?), but once the script ends, the shell also exits and whatever aliases could have been defined in the ~/.bash_aliases
file, they are gone.
That's the difference between running a shell script and sourcing it. If you run a shell script, it runs in it's own shell, so any changes to environment, aliases and the similar done in the script do not affect the parent shell (the shell the script was started from). But when you source the same script, it executes in the current shell, so all changes it makes stay after the script finishes (of course they stay as long as the shell lives - if you exit the shell, all changes are gone again).
Taking this into account, if you want aliases from the file /etc/.bash_aliases
to be available to any user in any shell they run, the file must be sourced from /etc/bash.bashrc
file. This file is executed whenever any shell (from any user) starts and is commonly used to set the initial environment.
So if you add the following line at the end of /etc/bash.bashrc
(of course you must do it as root, to be able to edit the file):
source /etc/.bash_aliases
you should get what you want.
The alias.sh
file isn't needed at all.