I want to use some aliases in a non-interactive shell, after reading this post and this post. Here is my command:

bash -O expand_aliases -c 'source ~/.bashrc; <some alias>'

and here is my .bashrc:

 # Bash History
 # Colorful Prompt
 PS1='\[\033[01;34m\]\w\n\[\033[01;32m\]\u@\h\[\033[00m\] > '
 # Aliases
 alias ls='ls --color=auto -A -I NTUSER.\* -I ntuser.\*'
 alias echo='echo -e'
 alias docker='"/mnt/c/Program Files/Docker/Docker/resources/bin/docker.exe"'
 alias docker-compose='"/mnt/c/Program Files/Docker/Docker/resources/bin/docker-compose.exe"'

I echoed something in .bashrc and am sure that source ~/.bashrc worked, but the aliases just refused to expand. I also tried:

bash -O expand_aliases -c shopt

to ensure that the option expand_aliases is set to on.

How do I fix this?


The very simple workaround is to use a function instead of an alias. There are many situations where functions are superior to aliases, and as far as I can tell none where the opposite is true.

 docker () { "/mnt/c/Program Files/Docker/Docker/resources/bin/docker.exe" "$@"; }
 docker-compose () { "/mnt/c/Program Files/Docker/Docker/resources/bin/docker-compose.exe" "$@"; }

(I have refused to port your ls and echo aliases - they seem more harm than good.)


The .bashrc is executed for interactive shells only.

This is what I've done. Create a new file .bash_env in your home directory. Put all your aliases there, e.g.:

shopt -s expand_aliases
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'

Then reference it in your .bash_profile (put a real pathname there, of course):

 export BASH_ENV='/home/username/.bash_env'

(Log out and log in to activate)

BASH_ENV in the manpage:

If this parameter is set when bash is executing a shell script, its value is interpreted as a filename containing commands to initialize the shell, as in ~/.bashrc.


When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.