Registering an alias via shell script


I have following shell script

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo alias mymanager=$DIR/mymanager

The problem is, it displays command i have to copy and paste. And I cannot make it work to register alias automatically.

So Far I tried

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
eval alias mymanager=$DIR/mymanager
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
alias mymanager=$DIR/mymanager

However, mymanager alias is not defined if I run it like this.


An alias is only valid in the current process. When you execute your script, a new child process is started (the binary specified in the shebang line), the script executed, and the process terminated. When the process terminates, your alias disappears with it. Child processes cannot modify the parent process (they cannot change variables, they cannot introduce aliases, they cannot change their parent’s working directory (cd)).

If you want to execute all lines of a script in your current shell process, use the source command (. for short). This will ignore the shebang interpreter and directly load the file.

. ./ # or:
source ./

Answered By – knittl

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published