0

I already have a bash script acting as a login shell, as an example:

#!/bin/bash

echo "this is a login shell"
# do some other initialization

bash

Now I want to execute this script at other occurrences too but it should behave differently, such as:

#!/bin/bash

is_login_shell() {
    # ???
}

if is_login_shell; then
    echo "this is a login shell"
    # do some other initialization
else
    echo "this is not a login shell"
fi

bash

How to achieve this detection? I've already tried [[ $0 == -* ]] but it seems to be not working as $0 will be /bin/bash.

Assume environment is a modern Linux distribution.

Edit: In my use case I also need to detect if the script is invoked directly as a login shell, or some user login with a bash shell and sourced this script in their login shell.

Browse other questions tagged or ask your own question.