question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bash should be run as a login shell

See original GitHub issue

It looks like bash understands $SHELL but this shell is not a login shell which means people who have customized their $PROMPT_COMMAND in their Dotfiles end up with weird errors.

A couple of solutions I can think of.

  1. Run bash with --login --interactive
  2. Rather than just running $SHELL, you can run exec and source the virtualenv.
    • This has the added bonus of having the same virtualenv flow and running `deactivate.
    • Running Ctrl-D is error prone, I quit the shell thinking I’m inside pipenv shell many many times.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
jjangsangycommented, Jun 14, 2017

Just read through some of the pew docs (that was helpful), so I’m looking right here in the troubleshoot section.

They recommend that you move your PATH declarations into ~/.profile and execute as a login shell.

Also, this table was really helpful me figure out why ~/.profile was not getting sourced

Mode/etc/profile~/.bash_profile~/.bash_<span class="search_hit mark">login</span>~/.profile~/.bashrc${ENV}
<span class="search_hit mark">Login</span> <span class="search_hit mark">shell</span> X X X X - -
Interactive Shell - - - - X -
SH compatible <span class="search_hit mark">login</span> X - - X - -
SH compatible - - - - - X
POSIX® compatiblity - - - - - X
3reactions
jjangsangycommented, Jun 15, 2017

After reading the pew docs and combing deep caverns of shell scripting lore I discovered that functions can be exported (who knew that was a thing!).

function export example

So let’s say I have function that fork bombs your shell and I want to hot potato it into a child shell processes.

$ function forking { forking | forking & }
$ pipenv shell
$ foobar
bash: foobar: command not found

Keep forking to a minimum

Bash prevents child processes from cases of unprotected forking somtimes…. You need to explicitly flag functions using export -f foobar or declare -fx foobar.

$ declare -F
declare -fx powerline_precmd

Environment is copy by value

Even if the functions are bound to environment variables that are themselves exported, the new sub shell is passed a new copy of the functions and not a reference.

A good example is ${PROMPT_COMMAND} which is usually bound to a function that populates the variable. That function will get called every time $PS1 is displayed so make sure to export it.

function powerline_precmd() {
     export PS1="$(powerline-shell 2> /dev/null)"
}
declare -fx powerline_precmd
export PROMPT_COMMAND="powerline_precmd"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Difference between Login Shell and Non-Login Shell?
From bash manual, A login shell is one whose first character of argument zero is '-', or one invoked with the --login option....
Read more >
What does "Run command as a login shell" do? - Ask Ubuntu
When running as a login shell, Bash will read ~/.bash_profile (or, if that doesn't exist, ~/.profile ) on startup. In some cases, this...
Read more >
What is Login Shell in Linux?
The login shell is the first process that is executed with your user ID when you log into an interactive session.
Read more >
Login shells
A login shell is a shell given to a user upon login into their user account. This is initiated by using the -l...
Read more >
Invoking Bash (Bash Reference Manual) - GNU.org
When the shell is interactive, this is equivalent to starting a login shell with ' exec -l bash '. When the shell is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found