2

When I run in my terminal:

alias

a list with all my aliases (defined in ~/bashrc and ~/.bash_aliases files) will be displayed on my terminal. That's nice and as expected!

But when I run:

bash -c "alias"

there is no output, so no aliases. First I thought that ~/.bashrc file is not sourced in the second case, so I ran:

bash -c ". ~/.bashrc && alias"

but, stupor, again there is no output...

Strangely enough, when I run:

bash -c ". ~/.bash_aliases && alias"

only the aliases defined in ~/.bash_aliases will be displayed.

Can someone make some light and make me understand what exactly it is happening here?

  • bash -c ". ~/.bashrc && alias" shows the aliases for me. Are you sure it does not work? – Bernhard Jul 16 '14 at 19:44
  • @Bernhard Yep... Ubuntu 14.04, bash 4.3.11(1)-release (x86_64-pc-linux-gnu) – Radu Rădeanu Jul 16 '14 at 19:47
  • Exactly my configuration at the moment :) – Bernhard Jul 16 '14 at 19:49
  • @Bernhard I have in my .bashrc file the following code somewhere at the beginning # If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac . You don't have it? ...So Cyrus answer exactly to my Q. – Radu Rădeanu Jul 16 '14 at 20:19
5

You need an interactive shell for alias definitions:

bash -i -c "alias"
  • Well spotted! You can add also to your answer the fact that .bashrc file will return immediately if not running interactively (see this comment). – Radu Rădeanu Jul 16 '14 at 20:23

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.