Get the path of sourced bash script [duplicate]2019 Community Moderator Electiondetermining path to sourced shell scriptPrinting (saving) the last bash input commandhow to get a variable's definition filebash add path to handle some filesbash script locked at if statement when executedFilename expansion for expect script within bashPreform operation in bash only if a variable is less than a second variableBash script failing to call other script without errorexecute command with sudo and execute Bash script with sudoHow to find Bash script location from within, when it's being launched as PBS job?Keep sourced file after script execution

Did I make a mistake by ccing email to boss to others?

Why is indicated airspeed rather than ground speed used during the takeoff roll?

PTIJ: Which Dr. Seuss books should one obtain?

What should be the ideal length of sentences in a blog post for ease of reading?

How to split IPA spelling into syllables

Unfrosted light bulb

Is this saw blade faulty?

I keep switching characters, how do I stop?

Highest stage count that are used one right after the other?

Put the phone down / Put down the phone

Rendered textures different to 3D View

Why does a 97 / 92 key piano exist by Bosendorfer?

Strange behavior in TikZ draw command

Do native speakers use "ultima" and "proxima" frequently in spoken English?

Why can't I get pgrep output right to variable on bash script?

Amorphous proper classes in MK

Can a Knock spell open the door to Mordenkainen's Magnificent Mansion?

Does capillary rise violate hydrostatic paradox?

When is the exact date for EOL of Ubuntu 14.04 LTS?

Should I warn a new PhD Student?

Is there a distance limit for minecart tracks?

Why do Radio Buttons not fill the entire outer circle?

Error in master's thesis, I do not know what to do

What is it called when someone votes for an option that's not their first choice?



Get the path of sourced bash script [duplicate]



2019 Community Moderator Electiondetermining path to sourced shell scriptPrinting (saving) the last bash input commandhow to get a variable's definition filebash add path to handle some filesbash script locked at if statement when executedFilename expansion for expect script within bashPreform operation in bash only if a variable is less than a second variableBash script failing to call other script without errorexecute command with sudo and execute Bash script with sudoHow to find Bash script location from within, when it's being launched as PBS job?Keep sourced file after script execution










0
















This question already has an answer here:



  • determining path to sourced shell script

    10 answers



There is my simple script that I try




#!/bin/bash
DIR="$(cd "$(dirname $0)" && pwd)"
echo $DIR




When I execute it like $ ./my_script.sh, get the path correctly.

But, when I source it like $ source my_script.sh, it gets the path /bin



How can I get the path of script by source it?

What is the different between source and execute?










share|improve this question







New contributor




NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
14 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    0
















    This question already has an answer here:



    • determining path to sourced shell script

      10 answers



    There is my simple script that I try




    #!/bin/bash
    DIR="$(cd "$(dirname $0)" && pwd)"
    echo $DIR




    When I execute it like $ ./my_script.sh, get the path correctly.

    But, when I source it like $ source my_script.sh, it gets the path /bin



    How can I get the path of script by source it?

    What is the different between source and execute?










    share|improve this question







    New contributor




    NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    marked as duplicate by Kusalananda bash
    Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    14 hours ago


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      0












      0








      0









      This question already has an answer here:



      • determining path to sourced shell script

        10 answers



      There is my simple script that I try




      #!/bin/bash
      DIR="$(cd "$(dirname $0)" && pwd)"
      echo $DIR




      When I execute it like $ ./my_script.sh, get the path correctly.

      But, when I source it like $ source my_script.sh, it gets the path /bin



      How can I get the path of script by source it?

      What is the different between source and execute?










      share|improve this question







      New contributor




      NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.













      This question already has an answer here:



      • determining path to sourced shell script

        10 answers



      There is my simple script that I try




      #!/bin/bash
      DIR="$(cd "$(dirname $0)" && pwd)"
      echo $DIR




      When I execute it like $ ./my_script.sh, get the path correctly.

      But, when I source it like $ source my_script.sh, it gets the path /bin



      How can I get the path of script by source it?

      What is the different between source and execute?





      This question already has an answer here:



      • determining path to sourced shell script

        10 answers







      bash shell-script






      share|improve this question







      New contributor




      NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 15 hours ago









      NEETNEET

      32




      32




      New contributor




      NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      NEET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      marked as duplicate by Kusalananda bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      14 hours ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Kusalananda bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      14 hours ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          1 Answer
          1






          active

          oldest

          votes


















          1














          The difference between sourcing a a script and "running it" is that when you source it (using source or .), the script is executed in the current shell environment, while if you "run it", a new shell process is started. You want to source scripts that you want would change the current shell environment. A script that is run in a separate shell process can not change the parent shell's environment. By "environment" is meant, for example, values of shell and environment variables, the current working directory etc.



          A script is usually written to either be sourced or to be executed in its own shell environment, but very seldom both. A script that is made to be sourced is sometimes called a "dot-script" (since . is the standard command for sourcing such a script; source is a bash "alias" for .).



          A dot-script being sourced by a bash shell can find its location by examining the first element of the BASH_SOURCE array:



          printf 'My location: %sn' "$( dirname "$BASH_SOURCE[0]" )"


          The directory path will be relative to the current working directory at the time of invoking source or . on the script.






          share|improve this answer























          • Thank you! I benefited a lot.

            – NEET
            14 hours ago

















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The difference between sourcing a a script and "running it" is that when you source it (using source or .), the script is executed in the current shell environment, while if you "run it", a new shell process is started. You want to source scripts that you want would change the current shell environment. A script that is run in a separate shell process can not change the parent shell's environment. By "environment" is meant, for example, values of shell and environment variables, the current working directory etc.



          A script is usually written to either be sourced or to be executed in its own shell environment, but very seldom both. A script that is made to be sourced is sometimes called a "dot-script" (since . is the standard command for sourcing such a script; source is a bash "alias" for .).



          A dot-script being sourced by a bash shell can find its location by examining the first element of the BASH_SOURCE array:



          printf 'My location: %sn' "$( dirname "$BASH_SOURCE[0]" )"


          The directory path will be relative to the current working directory at the time of invoking source or . on the script.






          share|improve this answer























          • Thank you! I benefited a lot.

            – NEET
            14 hours ago















          1














          The difference between sourcing a a script and "running it" is that when you source it (using source or .), the script is executed in the current shell environment, while if you "run it", a new shell process is started. You want to source scripts that you want would change the current shell environment. A script that is run in a separate shell process can not change the parent shell's environment. By "environment" is meant, for example, values of shell and environment variables, the current working directory etc.



          A script is usually written to either be sourced or to be executed in its own shell environment, but very seldom both. A script that is made to be sourced is sometimes called a "dot-script" (since . is the standard command for sourcing such a script; source is a bash "alias" for .).



          A dot-script being sourced by a bash shell can find its location by examining the first element of the BASH_SOURCE array:



          printf 'My location: %sn' "$( dirname "$BASH_SOURCE[0]" )"


          The directory path will be relative to the current working directory at the time of invoking source or . on the script.






          share|improve this answer























          • Thank you! I benefited a lot.

            – NEET
            14 hours ago













          1












          1








          1







          The difference between sourcing a a script and "running it" is that when you source it (using source or .), the script is executed in the current shell environment, while if you "run it", a new shell process is started. You want to source scripts that you want would change the current shell environment. A script that is run in a separate shell process can not change the parent shell's environment. By "environment" is meant, for example, values of shell and environment variables, the current working directory etc.



          A script is usually written to either be sourced or to be executed in its own shell environment, but very seldom both. A script that is made to be sourced is sometimes called a "dot-script" (since . is the standard command for sourcing such a script; source is a bash "alias" for .).



          A dot-script being sourced by a bash shell can find its location by examining the first element of the BASH_SOURCE array:



          printf 'My location: %sn' "$( dirname "$BASH_SOURCE[0]" )"


          The directory path will be relative to the current working directory at the time of invoking source or . on the script.






          share|improve this answer













          The difference between sourcing a a script and "running it" is that when you source it (using source or .), the script is executed in the current shell environment, while if you "run it", a new shell process is started. You want to source scripts that you want would change the current shell environment. A script that is run in a separate shell process can not change the parent shell's environment. By "environment" is meant, for example, values of shell and environment variables, the current working directory etc.



          A script is usually written to either be sourced or to be executed in its own shell environment, but very seldom both. A script that is made to be sourced is sometimes called a "dot-script" (since . is the standard command for sourcing such a script; source is a bash "alias" for .).



          A dot-script being sourced by a bash shell can find its location by examining the first element of the BASH_SOURCE array:



          printf 'My location: %sn' "$( dirname "$BASH_SOURCE[0]" )"


          The directory path will be relative to the current working directory at the time of invoking source or . on the script.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 14 hours ago









          KusalanandaKusalananda

          136k17257426




          136k17257426












          • Thank you! I benefited a lot.

            – NEET
            14 hours ago

















          • Thank you! I benefited a lot.

            – NEET
            14 hours ago
















          Thank you! I benefited a lot.

          – NEET
          14 hours ago





          Thank you! I benefited a lot.

          – NEET
          14 hours ago



          Popular posts from this blog

          getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

          Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

          Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.