Collecting the origin directory of a Bash script from itself [duplicate]












2
















This question already has an answer here:




  • When writing a bash script, how do I get the absolute path of the location of the current file?

    3 answers




Is there a way that a bash script detects the path directory in which it's running? It is like if the script is aware of where is it.
In order to use files that are in the same location as the script. I'd like to change the directory to where the Bash Script is.



This Bash script is used to trigger few actions (other python scripts, apps... etc.) but I want to switch the operation directory to his root one.










share|improve this question















marked as duplicate by RoVo, Rinzwind, karel, Arronical, Sergiy Kolodyazhnyy 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();
}
);
});
});
Jan 16 at 10:51


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.



















  • @RoVO, well it's not exactly a duplicate because it's on a different site. That said, the answer to that question on StackOverflow is exactly what the OP needs ;)

    – Mr Shunz
    Jan 16 at 9:56











  • @RoVo yes it is.

    – Mr Shunz
    Jan 16 at 9:58











  • Considering that OP themselves agreed the post is duplicate, it's closed now. However, if necessary this can be reopened. Just let us know

    – Sergiy Kolodyazhnyy
    Jan 16 at 10:51
















2
















This question already has an answer here:




  • When writing a bash script, how do I get the absolute path of the location of the current file?

    3 answers




Is there a way that a bash script detects the path directory in which it's running? It is like if the script is aware of where is it.
In order to use files that are in the same location as the script. I'd like to change the directory to where the Bash Script is.



This Bash script is used to trigger few actions (other python scripts, apps... etc.) but I want to switch the operation directory to his root one.










share|improve this question















marked as duplicate by RoVo, Rinzwind, karel, Arronical, Sergiy Kolodyazhnyy 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();
}
);
});
});
Jan 16 at 10:51


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.



















  • @RoVO, well it's not exactly a duplicate because it's on a different site. That said, the answer to that question on StackOverflow is exactly what the OP needs ;)

    – Mr Shunz
    Jan 16 at 9:56











  • @RoVo yes it is.

    – Mr Shunz
    Jan 16 at 9:58











  • Considering that OP themselves agreed the post is duplicate, it's closed now. However, if necessary this can be reopened. Just let us know

    – Sergiy Kolodyazhnyy
    Jan 16 at 10:51














2












2








2


2







This question already has an answer here:




  • When writing a bash script, how do I get the absolute path of the location of the current file?

    3 answers




Is there a way that a bash script detects the path directory in which it's running? It is like if the script is aware of where is it.
In order to use files that are in the same location as the script. I'd like to change the directory to where the Bash Script is.



This Bash script is used to trigger few actions (other python scripts, apps... etc.) but I want to switch the operation directory to his root one.










share|improve this question

















This question already has an answer here:




  • When writing a bash script, how do I get the absolute path of the location of the current file?

    3 answers




Is there a way that a bash script detects the path directory in which it's running? It is like if the script is aware of where is it.
In order to use files that are in the same location as the script. I'd like to change the directory to where the Bash Script is.



This Bash script is used to trigger few actions (other python scripts, apps... etc.) but I want to switch the operation directory to his root one.





This question already has an answer here:




  • When writing a bash script, how do I get the absolute path of the location of the current file?

    3 answers








bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 16 at 11:50









Alexandr Gavriliuc

31




31










asked Jan 16 at 9:45









Fouad DjebbarFouad Djebbar

135




135




marked as duplicate by RoVo, Rinzwind, karel, Arronical, Sergiy Kolodyazhnyy 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();
}
);
});
});
Jan 16 at 10:51


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 RoVo, Rinzwind, karel, Arronical, Sergiy Kolodyazhnyy 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();
}
);
});
});
Jan 16 at 10:51


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.















  • @RoVO, well it's not exactly a duplicate because it's on a different site. That said, the answer to that question on StackOverflow is exactly what the OP needs ;)

    – Mr Shunz
    Jan 16 at 9:56











  • @RoVo yes it is.

    – Mr Shunz
    Jan 16 at 9:58











  • Considering that OP themselves agreed the post is duplicate, it's closed now. However, if necessary this can be reopened. Just let us know

    – Sergiy Kolodyazhnyy
    Jan 16 at 10:51



















  • @RoVO, well it's not exactly a duplicate because it's on a different site. That said, the answer to that question on StackOverflow is exactly what the OP needs ;)

    – Mr Shunz
    Jan 16 at 9:56











  • @RoVo yes it is.

    – Mr Shunz
    Jan 16 at 9:58











  • Considering that OP themselves agreed the post is duplicate, it's closed now. However, if necessary this can be reopened. Just let us know

    – Sergiy Kolodyazhnyy
    Jan 16 at 10:51

















@RoVO, well it's not exactly a duplicate because it's on a different site. That said, the answer to that question on StackOverflow is exactly what the OP needs ;)

– Mr Shunz
Jan 16 at 9:56





@RoVO, well it's not exactly a duplicate because it's on a different site. That said, the answer to that question on StackOverflow is exactly what the OP needs ;)

– Mr Shunz
Jan 16 at 9:56













@RoVo yes it is.

– Mr Shunz
Jan 16 at 9:58





@RoVo yes it is.

– Mr Shunz
Jan 16 at 9:58













Considering that OP themselves agreed the post is duplicate, it's closed now. However, if necessary this can be reopened. Just let us know

– Sergiy Kolodyazhnyy
Jan 16 at 10:51





Considering that OP themselves agreed the post is duplicate, it's closed now. However, if necessary this can be reopened. Just let us know

– Sergiy Kolodyazhnyy
Jan 16 at 10:51










1 Answer
1






active

oldest

votes


















2














The $PWD variable (which is also queried by pwd command) will tell you current working directory where the shell script operates. However, finding where the script itself is located depends on couple factors.



If you are using bash shell, you may use ${BASH_SOURCE[0]} variable for the purpose of finding where the script runs, as shown by Dave Dopson:



DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"


In other cases, such as zsh (unless in a function), ksh, and POSIX /bin/sh the $0 variable should be queried, probably with the use of dirname command, or via parameter substitution. Note that this applies mostly to the top-level scripts that you execute.



However, sourced scripts can be a problem, if you're not using bash and ${BASH_SOURCE[0]} variable. According to Gilles's answer, location of sourced scripts is not available in general from within the script itself. The calling script will know the location, but the sourced script - won't. However, the cited answer by Gilles offers a few solutions. There are a few ways to portably determine if the script is sourced, however it seems the only benefit of that is to determine that script location is not available.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The $PWD variable (which is also queried by pwd command) will tell you current working directory where the shell script operates. However, finding where the script itself is located depends on couple factors.



    If you are using bash shell, you may use ${BASH_SOURCE[0]} variable for the purpose of finding where the script runs, as shown by Dave Dopson:



    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"


    In other cases, such as zsh (unless in a function), ksh, and POSIX /bin/sh the $0 variable should be queried, probably with the use of dirname command, or via parameter substitution. Note that this applies mostly to the top-level scripts that you execute.



    However, sourced scripts can be a problem, if you're not using bash and ${BASH_SOURCE[0]} variable. According to Gilles's answer, location of sourced scripts is not available in general from within the script itself. The calling script will know the location, but the sourced script - won't. However, the cited answer by Gilles offers a few solutions. There are a few ways to portably determine if the script is sourced, however it seems the only benefit of that is to determine that script location is not available.






    share|improve this answer




























      2














      The $PWD variable (which is also queried by pwd command) will tell you current working directory where the shell script operates. However, finding where the script itself is located depends on couple factors.



      If you are using bash shell, you may use ${BASH_SOURCE[0]} variable for the purpose of finding where the script runs, as shown by Dave Dopson:



      DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"


      In other cases, such as zsh (unless in a function), ksh, and POSIX /bin/sh the $0 variable should be queried, probably with the use of dirname command, or via parameter substitution. Note that this applies mostly to the top-level scripts that you execute.



      However, sourced scripts can be a problem, if you're not using bash and ${BASH_SOURCE[0]} variable. According to Gilles's answer, location of sourced scripts is not available in general from within the script itself. The calling script will know the location, but the sourced script - won't. However, the cited answer by Gilles offers a few solutions. There are a few ways to portably determine if the script is sourced, however it seems the only benefit of that is to determine that script location is not available.






      share|improve this answer


























        2












        2








        2







        The $PWD variable (which is also queried by pwd command) will tell you current working directory where the shell script operates. However, finding where the script itself is located depends on couple factors.



        If you are using bash shell, you may use ${BASH_SOURCE[0]} variable for the purpose of finding where the script runs, as shown by Dave Dopson:



        DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"


        In other cases, such as zsh (unless in a function), ksh, and POSIX /bin/sh the $0 variable should be queried, probably with the use of dirname command, or via parameter substitution. Note that this applies mostly to the top-level scripts that you execute.



        However, sourced scripts can be a problem, if you're not using bash and ${BASH_SOURCE[0]} variable. According to Gilles's answer, location of sourced scripts is not available in general from within the script itself. The calling script will know the location, but the sourced script - won't. However, the cited answer by Gilles offers a few solutions. There are a few ways to portably determine if the script is sourced, however it seems the only benefit of that is to determine that script location is not available.






        share|improve this answer













        The $PWD variable (which is also queried by pwd command) will tell you current working directory where the shell script operates. However, finding where the script itself is located depends on couple factors.



        If you are using bash shell, you may use ${BASH_SOURCE[0]} variable for the purpose of finding where the script runs, as shown by Dave Dopson:



        DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"


        In other cases, such as zsh (unless in a function), ksh, and POSIX /bin/sh the $0 variable should be queried, probably with the use of dirname command, or via parameter substitution. Note that this applies mostly to the top-level scripts that you execute.



        However, sourced scripts can be a problem, if you're not using bash and ${BASH_SOURCE[0]} variable. According to Gilles's answer, location of sourced scripts is not available in general from within the script itself. The calling script will know the location, but the sourced script - won't. However, the cited answer by Gilles offers a few solutions. There are a few ways to portably determine if the script is sourced, however it seems the only benefit of that is to determine that script location is not available.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 16 at 10:05









        Sergiy KolodyazhnyySergiy Kolodyazhnyy

        73.2k9153317




        73.2k9153317















            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?