In my shell environment I have aliases and custom functions. When I am in an instance of emacs (I always use emacs -nw) and I execute a shell command (M-!
We need to enable alias expansion in non-interactive shells, preferably only when they are called from emacs (to avoid causing subtle differences when we run other shells). What worked for me was to create two files: ~/.alias has all my aliases, e.g.
alias python python27
and ~/.aliasExpand has
source ~/.alias
shopt -s expand_aliases
I also, of course, replaced all my aliases in .bashrc with
source ~/.alias
Finally, I added this to my .emacs:
(setenv "BASH_ENV" "~/.aliasExpand")
I tried Keith Flower's solution (of making shells interactive), and I got
bash: cannot set terminal process group (-1): Inappropriate ioctl for device bash: no job control in this shell
Note that I got this through Glenn Jackman's and Nicholas Dudebout's hints.
If you are willing to risk having all your shells have alias expansion and you don't mind running all your .bashrc for every emacs shell, you can simplify this to adding
shopt -s expand_aliases
to your .bashrc and
(setenv "BASH_ENV" "~/.bashrc")
to your .emacs