Aliases in subshell / child process
I set up aliases in /etc/profile.d/alias.sh for each login shell. But if I run script.sh, I can't use that alias. How can I set alias even for subshells or child processes ?
/etc/profile.d/alias.sh
alias rmvr='rm -rv';
alias cprv='cp -rv';
alias mvrv='mv -rv';
Solution 1:
Aliases are not inherited. That's why they are traditionally set in bashrc
and not profile
. Source your script.sh
from your .bashrc
or the system-wide one instead.
Solution 2:
If you want them to be inherited to sub-shells, use functions instead. Those can be exported to the environment (export -f
), and sub-shells will then have those functions defined.
So, for one of your examples:
rmvr() { rm -rv "$@"; }
export -f rmvr
If you have a bunch of them, then set for export first:
set -a # export the following funcs
rmvr() { rm -rv "$@"; }
cpvr() { cp -rv "$@"; }
mvrv() { mv -rv "$@"; }
set +a # stop exporting