I'm trying to run a non-interactive command within a login shell (using su - or sudo -i) but couldn't make it work so far.
Within ~/.bashrc I have sourced a script which sets all variables my application needs. When I do an interactive login su - username I can see all my variables there and if I run my application it works fine. However, when I try to execute my application by using su - username -c 'command' the necessary variables are not being set. In other words, it seems su - is incompatible with -c option. The same happens with sudo -i -u user command.
I have searched tons of questions but couldn't find an answer to this specific behavior.
According to bash man pages:
When bash is invoked as an interactive login shell, or as a non-interactive
shell with the `--login` option, it first reads and executes commands from the
file `/etc/profile`, if that file exists. After reading that file, it looks for
`~/.bash_profile`, `~/.bash_login`, and `~/.profile`, in that order, and reads and
executes commands from the first one that exists and is readable. The
`--noprofile` option may be used when the shell is started to inhibit this
behavior.
The above behavior is not working as ~/.profile executes ~/.bashrc and it in turn sources the script which sets all variables.
How can I execute a non-interactive login shell?
Thank you