using alias in shell script? [duplicate]

source your script, don't execute it like ./foo.sh or sh foo.sh

If you execute your script like that, it is running in sub-shell, not your current.

source foo.sh  

would work for you.


You need to set a specific option to do so, expand_aliases:

 shopt -s expand_aliases

Exemple:

# With option
$ cat a
#!/bin/bash
shopt -s expand_aliases
alias a="echo b"
type a
a
$ ./a
a is aliased to 'echo b'
b

# Without option
$ cat a
#!/bin/bash
alias a="echo b"
type a
a

$ ./a
./a: line 3: type: a: not found
./a: line 4: a: command not found

cf: https://unix.stackexchange.com/a/1498/27031 and https://askubuntu.com/a/98786/127746