Aliases in subshell / child process

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.


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

It is because /etc/profile.d/ is used only by interactive login shell. However, /etc/bash.bashrc is used by interactive non-login shell.

As I usually do set some global aliases for system, I have started to create /etc/bashrc.d where I can drop a file with some global aliases:

    HAVE_BASHRC_D=`cat /etc/bash.bashrc | grep -F '/etc/bashrc.d' | wc -l`

    if [ ! -d /etc/bashrc.d ]; then
            mkdir -p /etc/bashrc.d
    fi
    if [ "$HAVE_BASHRC_D" == "0" ]; then
        echo "Setting up bash aliases"
            (cat <<-'EOF'
                                    if [ -d /etc/bashrc.d ]; then
                                      for i in /etc/bashrc.d/*.sh; do
                                        if [ -r $i ]; then
                                          . $i
                                        fi
                                      done
                                      unset i
                                    fi
                            EOF
            ) >> /etc/bash.bashrc

    fi

Tags:

Linux

Shell

Alias