0

I have added echo thisIsMyBash at the end of my .bashrc file. Now when I write create a new terminal with ctrl+alt+t, thisIsMyBash is written a the beginning. This is as expected.

Now when I type /bin/bash in my terminal thisIsMyBash is also displayed in my terminal.

But when I write /bin/bash -c "source ~/.bashrc" in my terminal, nothing is written. Why?

Also when I actually write /bin/bash -c "source ~/.bashrc" && exec /bin/bash -li thisIsMyBash also is displayed. Why that?

Any idea?

arennuit
  • 855
  • 1
  • 7
  • 23
  • 2
    Is there anything in your `.bashrc` file that causes it to exit early if it isn't an interactive shell? – chepner Nov 17 '17 at 15:16
  • Not really a programming question, but in any case I cannot reproduce this behavior. Perhaps you have some logic earlier in your bashrc that causes it to stop executing before the echo under certain conditions? In a clean shell with nothing but the echo in the bashrc, I get it every time, including when run via `bash -c source`. – Mark Reed Nov 17 '17 at 15:16
  • @MarkReed the behavior was easy to reproduce for me. Just placed echo at the end end just got the same result. – Fausto Carvalho Marques Silva Nov 17 '17 at 15:27
  • Possible duplicate of [What's the difference between .bashrc, .bash\_profile, and .environment?](https://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment) – Joe Nov 17 '17 at 15:32

1 Answers1

2

If you use the "-i" option of bash it will work as desired. This option make the shell interactive. Try this:

/bin/bash -i -c "source ~/.bashrc"

Take a look here.

  • This implies that there is code in the bashrc testing for interactivity and behaving differently when it is found. That context was not present in the question. – Mark Reed Nov 17 '17 at 20:12