Is there a way to expand aliases in a non-interactive sh shell?

1.6k views Asked by At

I have a collection of aliases defined in ~/.aliases which I would like to make available to sh even when it is run non-interactively. My system has been setup in the typical way so that sh is a symlink to bash.

When bash is run non-interactively as bash, this could by using shopt -s expand_aliases together with setting $ENV or $BASH_ENV to (directly or indirectly) source ~/.aliases.

But when bash is invoked non-interactively as sh, it seems to ignore $ENV and all startup files, so I can't see a way to do it. Any ideas? Or is this just not possible?

1

There are 1 answers

0
jcollado On

One way to force the shell to be interactive when running a script is using -i such as:

$ bash -i <script>

Also, note that if your script has execute permissions, you can replace:

#!/bin/bash

with:

#!/bin/bash -i