Bash – run bash script doesn’t work alias command in zsh

aliasbashscriptingshell

I use zsh in centos. And I want running some shell script. Like this.

script.sh

#!/bin/sh

# something ..

source ./aliases-file.sh

aliases-file.sh

alias test=ls
alias test2=cat

I run ./script.sh, the alias has not been applied to the current session.
how can I work this?

Best Answer

When you run script.sh it is executed in a subshell and your aliases won't propagate up to the calling shell (or current session as you refer to it). You need to source script.sh itself: source script.sh. That will cause the commands to be run in your current shell and the aliases will become visible.

Side note: for portability reasons you should consider using . instead of source as the former is POSIX standard while the latter is a bashism.

Related Question