What are the pro's and con's in using the “-l” in a script shebang

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as:



#!/bin/bash -l 






share|improve this question


















  • 1




    I do wonder how being a login shell was even related to your issue there..
    – ilkkachu
    Feb 7 at 10:16










  • Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
    – Pier
    Feb 18 at 17:26














up vote
0
down vote

favorite












I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as:



#!/bin/bash -l 






share|improve this question


















  • 1




    I do wonder how being a login shell was even related to your issue there..
    – ilkkachu
    Feb 7 at 10:16










  • Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
    – Pier
    Feb 18 at 17:26












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as:



#!/bin/bash -l 






share|improve this question














I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as:



#!/bin/bash -l 








share|improve this question













share|improve this question




share|improve this question








edited Feb 7 at 23:49









Jeff Schaller

31.3k846105




31.3k846105










asked Feb 7 at 10:02









Pier

65112




65112







  • 1




    I do wonder how being a login shell was even related to your issue there..
    – ilkkachu
    Feb 7 at 10:16










  • Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
    – Pier
    Feb 18 at 17:26












  • 1




    I do wonder how being a login shell was even related to your issue there..
    – ilkkachu
    Feb 7 at 10:16










  • Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
    – Pier
    Feb 18 at 17:26







1




1




I do wonder how being a login shell was even related to your issue there..
– ilkkachu
Feb 7 at 10:16




I do wonder how being a login shell was even related to your issue there..
– ilkkachu
Feb 7 at 10:16












Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
– Pier
Feb 18 at 17:26




Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
– Pier
Feb 18 at 17:26










2 Answers
2






active

oldest

votes

















up vote
6
down vote













[The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]



The -l option tells bash to read all the various "profile" scripts, from /etc and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).



Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.



A crontab is one example of running a script outside your session, so yes, go do it!



If the script is purely for the use of the crontab then adding -l to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:



0 * * * * bash -l hourly.sh





share|improve this answer




















  • Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
    – Kusalananda
    Feb 7 at 10:49










  • Thanks ams for your insight, that was some solution I did not think about.
    – Pier
    Feb 7 at 11:41










  • Also, if the user is doing something like this, there will be trouble.
    – Kusalananda
    Feb 8 at 20:25

















up vote
-1
down vote













I honestly don't see any benefit of running a non-interactive script in a login shell.



The login shell will parse the relevant login shell initialization files (bash uses ~/.bash_profile) to set up the shell session's environment etc.



It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux (like in this question) or even running exec on another shell altogether (as in this question, and this).



Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV. This file will be sourced by any non-interactive bash shell.



A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as



@daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"


(for a daily job triggered at midnight) here $HOME/script.env may be $HOME/.bashrc if that's where the environment is set up.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422499%2fwhat-are-the-pros-and-cons-in-using-the-l-in-a-script-shebang%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote













    [The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]



    The -l option tells bash to read all the various "profile" scripts, from /etc and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).



    Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.



    A crontab is one example of running a script outside your session, so yes, go do it!



    If the script is purely for the use of the crontab then adding -l to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:



    0 * * * * bash -l hourly.sh





    share|improve this answer




















    • Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
      – Kusalananda
      Feb 7 at 10:49










    • Thanks ams for your insight, that was some solution I did not think about.
      – Pier
      Feb 7 at 11:41










    • Also, if the user is doing something like this, there will be trouble.
      – Kusalananda
      Feb 8 at 20:25














    up vote
    6
    down vote













    [The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]



    The -l option tells bash to read all the various "profile" scripts, from /etc and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).



    Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.



    A crontab is one example of running a script outside your session, so yes, go do it!



    If the script is purely for the use of the crontab then adding -l to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:



    0 * * * * bash -l hourly.sh





    share|improve this answer




















    • Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
      – Kusalananda
      Feb 7 at 10:49










    • Thanks ams for your insight, that was some solution I did not think about.
      – Pier
      Feb 7 at 11:41










    • Also, if the user is doing something like this, there will be trouble.
      – Kusalananda
      Feb 8 at 20:25












    up vote
    6
    down vote










    up vote
    6
    down vote









    [The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]



    The -l option tells bash to read all the various "profile" scripts, from /etc and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).



    Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.



    A crontab is one example of running a script outside your session, so yes, go do it!



    If the script is purely for the use of the crontab then adding -l to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:



    0 * * * * bash -l hourly.sh





    share|improve this answer












    [The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]



    The -l option tells bash to read all the various "profile" scripts, from /etc and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).



    Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.



    A crontab is one example of running a script outside your session, so yes, go do it!



    If the script is purely for the use of the crontab then adding -l to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:



    0 * * * * bash -l hourly.sh






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 7 at 10:27









    ams

    4,1821123




    4,1821123











    • Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
      – Kusalananda
      Feb 7 at 10:49










    • Thanks ams for your insight, that was some solution I did not think about.
      – Pier
      Feb 7 at 11:41










    • Also, if the user is doing something like this, there will be trouble.
      – Kusalananda
      Feb 8 at 20:25
















    • Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
      – Kusalananda
      Feb 7 at 10:49










    • Thanks ams for your insight, that was some solution I did not think about.
      – Pier
      Feb 7 at 11:41










    • Also, if the user is doing something like this, there will be trouble.
      – Kusalananda
      Feb 8 at 20:25















    Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
    – Kusalananda
    Feb 7 at 10:49




    Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
    – Kusalananda
    Feb 7 at 10:49












    Thanks ams for your insight, that was some solution I did not think about.
    – Pier
    Feb 7 at 11:41




    Thanks ams for your insight, that was some solution I did not think about.
    – Pier
    Feb 7 at 11:41












    Also, if the user is doing something like this, there will be trouble.
    – Kusalananda
    Feb 8 at 20:25




    Also, if the user is doing something like this, there will be trouble.
    – Kusalananda
    Feb 8 at 20:25












    up vote
    -1
    down vote













    I honestly don't see any benefit of running a non-interactive script in a login shell.



    The login shell will parse the relevant login shell initialization files (bash uses ~/.bash_profile) to set up the shell session's environment etc.



    It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux (like in this question) or even running exec on another shell altogether (as in this question, and this).



    Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV. This file will be sourced by any non-interactive bash shell.



    A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as



    @daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"


    (for a daily job triggered at midnight) here $HOME/script.env may be $HOME/.bashrc if that's where the environment is set up.






    share|improve this answer


























      up vote
      -1
      down vote













      I honestly don't see any benefit of running a non-interactive script in a login shell.



      The login shell will parse the relevant login shell initialization files (bash uses ~/.bash_profile) to set up the shell session's environment etc.



      It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux (like in this question) or even running exec on another shell altogether (as in this question, and this).



      Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV. This file will be sourced by any non-interactive bash shell.



      A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as



      @daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"


      (for a daily job triggered at midnight) here $HOME/script.env may be $HOME/.bashrc if that's where the environment is set up.






      share|improve this answer
























        up vote
        -1
        down vote










        up vote
        -1
        down vote









        I honestly don't see any benefit of running a non-interactive script in a login shell.



        The login shell will parse the relevant login shell initialization files (bash uses ~/.bash_profile) to set up the shell session's environment etc.



        It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux (like in this question) or even running exec on another shell altogether (as in this question, and this).



        Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV. This file will be sourced by any non-interactive bash shell.



        A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as



        @daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"


        (for a daily job triggered at midnight) here $HOME/script.env may be $HOME/.bashrc if that's where the environment is set up.






        share|improve this answer














        I honestly don't see any benefit of running a non-interactive script in a login shell.



        The login shell will parse the relevant login shell initialization files (bash uses ~/.bash_profile) to set up the shell session's environment etc.



        It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux (like in this question) or even running exec on another shell altogether (as in this question, and this).



        Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV. This file will be sourced by any non-interactive bash shell.



        A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as



        @daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"


        (for a daily job triggered at midnight) here $HOME/script.env may be $HOME/.bashrc if that's where the environment is set up.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 8 at 21:06

























        answered Feb 8 at 20:43









        Kusalananda

        103k13202318




        103k13202318






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422499%2fwhat-are-the-pros-and-cons-in-using-the-l-in-a-script-shebang%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?