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
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?
bash shell-script
New contributor
marked as duplicate by Kusalananda
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.
add a comment |
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?
bash shell-script
New contributor
marked as duplicate by Kusalananda
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.
add a comment |
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?
bash shell-script
New contributor
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
bash shell-script
New contributor
New contributor
New contributor
asked 15 hours ago
NEETNEET
32
32
New contributor
New contributor
marked as duplicate by Kusalananda
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
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Thank you! I benefited a lot.
– NEET
14 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Thank you! I benefited a lot.
– NEET
14 hours ago
add a comment |
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.
Thank you! I benefited a lot.
– NEET
14 hours ago
add a comment |
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.
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.
answered 14 hours ago
KusalanandaKusalananda
136k17257426
136k17257426
Thank you! I benefited a lot.
– NEET
14 hours ago
add a comment |
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
add a comment |