Godzilla74 Godzilla74 - 11 months ago 80
Bash Question

Linux .profile overrides other bash commands

I am loading a script (whiptail) when the root user logs into their Linux server, which works fine. The thing is, now, when I attempt to run other scripts from the command prompt (or crontab) the initial script is loaded instead, and it looks like the script that I want to run is not.

This is what

looks like:

if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc

mesg n
source /root/menu.sh

So, when I try to run
bash -lc 'ruby some/other/script.rb
I'm taken into the script that runs at the end of
which is
. How can I keep this from happening?

Here's what I need to have happen in the long run:

  1. The server boots up and takes the user to

  2. There are background scripts that run via
    such as a check in script, job script, etc.

Answer Source

Best-practices: Don't use a login shell unless you need one

When you pass the -l argument to bash, you're telling it to behave as a login shell; this includes running the user's .profile.

If you don't want that behavior, don't pass -l. Thus:

bash -c 'ruby some/other/script.rb'

That said, there's no advantage to doing that over just invoking ruby directly, without any enclosing shell:

ruby some/other/script.rb

If you must use a login shell...

If you want other effects of running the user's .profile, you might set an environment variable to indicate that you want to bypass this behavior:

# in the user's login scripts
[ -n "$skip_menu" ] || source /root/menu.sh

...and then...

skip_menu=1 bash -lc '...your command here...'

...or, if being executed without an enclosing shell...

env skip_menu=1 bash -lc '...your command here...'