0

For any bash script on my macOS computer adding the --login argument to the any shebang, i.e. #!/bin/bash --login makes the script stall and never complete. For example, the simple script

#!/bin/bash --login
echo "hello"

will never exit. But remove --login argument from the shebang and it will exit as normal. Can someone please explain to me why this happens?

scruffaluff
  • 345
  • 4
  • 20
  • Why do you think you need this option in your shebang? What effect are you trying to achieve? – ghoti Jun 22 '19 at 05:02
  • The script is executed by a launchd service and needs to load my .bash_profile. – scruffaluff Jun 22 '19 at 05:04
  • It's likely that the contents of `.bash_profile` are relevant, then. – Benjamin W. Jun 22 '19 at 05:05
  • Good point. And yes it turns out that Anaconda is stalling the script with the common `eval "$__conda_setup"` command. Any clue as to why conda is stalling? – scruffaluff Jun 22 '19 at 05:17
  • This user posted the conda initialize script in his question for reference: https://stackoverflow.com/questions/54429210/how-do-i-prevent-conda-from-activating-the-base-environment-by-default – scruffaluff Jun 22 '19 at 05:18

1 Answers1

2

--login is for interactive shells. Your script isn't interactive so --login shouldn't be there.

If you want to load your .bash_profile you can source it:

#!/bin/bash

. ~/.bash_profile

echo hello
echo $SOME_VAR_IN_BASH_PROFILE
bartomeu
  • 486
  • 4
  • 5
  • --login is for non-iteractive shells. from the bash man page: `When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option` – scruffaluff Jun 22 '19 at 07:41
  • Also the source ( or .) command isn't necessarily available in launchd or systemd. So a login shell is necessary. – scruffaluff Jun 22 '19 at 07:47
  • @user7147804, I don't think that's correct. The `source` or `.` command is part of bash. If launchd runs a bash script, the interpretation of that script is done by bash, not by launchd. You may have *complications* associated with the location of the script or permissions or something, but if you're running a bash script, you can use bashisms in it. – ghoti Jun 22 '19 at 11:33
  • @ghoti Other commands such as brew and npm are unable to found with just sourcing ~/.bash_profile in a script executed by launchd. However they seem to be found with the --login option. Is there a way to get them to found without the --login option and also load my .bash_profile? – scruffaluff Jun 23 '19 at 03:43
  • @user7147804, there's more to this question than you're telling us. bmiro has correctly noted the method used for sourcing in bash, but you haven't shown us in your question what it was you tried that didn't work when you did it. I suspect you may have tried to use $HOME, which might be different for things launched by launchd. Try again, while watching the output of `tail -F /var/log/system.log`. As it stands, your question is missing the parts that would let folks give you a satisfactory answer. – ghoti Jun 23 '19 at 03:55
  • @ghoti I never asked for the method of sourcing in bash in the original question. I wanted to know why the --login argument causes all scripts to stall when executed from a basic command line. – scruffaluff Jun 23 '19 at 04:28
  • If you install things like `npm` with your user maybe they are installed to your user's home. If launchd or systemd run with the user root they won't find your install. Maybe you have to change the user of the systemd / launchd config that triggers your script. If `brew` / `npm` are installed system-wide maybe is a PATH problem so instead of using the command `brew` directly reference the full path on the script. If you give more scope of your problem it will be easier to understand. – bartomeu Jun 23 '19 at 06:42
  • @user7147804 ... yes, that's why I think this is an [XY Problem](https://mywiki.wooledge.org/XyProblem) (also [this](https://en.wikipedia.org/wiki/XY_problem) and [this](https://meta.stackexchange.com/a/66378/174723)). You should ask how to solve your problem, not how to implement the thing that you think might be a solution to your problem. – ghoti Jun 23 '19 at 17:15
  • @ghoti everything works perfectly fine when launchd executes the shell script with the --login option. However the shell script is unable to be executed from a basic command line. I do not need to execute the script from the command line. I simply want to know why it is unable to execute from the command line. I stated in the posting that my question is **why**. For future reference so I can better ask question, how should I ask my question so that is it clear that I am asking **why** the script stalls? – scruffaluff Jun 23 '19 at 18:13