Timeline for Obtain script current directory (so that I can do include files without relative paths and run the script from everywhere)
Current License: CC BY-SA 3.0
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 2, 2020 at 0:33 | comment | added | ian | @terdon Thank you, that's a very gracious apology, and my own comment history shows I fully understand the need for helpful coffee at times! :) My own apologies for misreading what you clearly didn't intend. Have a good day. | |
| Feb 28, 2020 at 11:29 | comment | added | terdon♦ |
@iain I'm sorry, I didn't mean to be dismissive (but on re-reading my comment I do see how it could have come across that way; my bad, I shouldn't answer comments before coffee). My suggestion to ask a new question was honestly offered. That's the best way to get the information you need and we try to avoid having conversations in the comments. Also, since I personally never call a script with shellName scriptName but always use shebangs instead, I admit I consider this a bit of an edge case. But that's my hangup.
|
|
| Feb 28, 2020 at 11:19 | comment | added | ian | I didn't say the answer was wrong with regards to the question but that it didn't work given a different way of calling it, one which is normal and valid, but more importantly, it's a helpful piece of information to add to an answer via comment. "of course" is not something I'd consider a valid response on a question and answer site, its whole reason for being is ignorance, but thanks for your hard work on the site, I managed to get what I wanted from another answer. | |
| Feb 28, 2020 at 9:32 | comment | added | terdon♦ |
@iain no, of course it won't work then, but that's a different issue. The question here was about executing the script by name. If you pass the script without a path as an argument to sh, then it's a totally different situation since $0 will not contain the path. It will work as expected if you use sh ./nameofscript.sh or sh /path/to/nameofscript.sh. If you need it to work specifically for when you call it with just the name and nothing else, please ask a new question.
|
|
| Feb 28, 2020 at 5:53 | comment | added | ian |
This didn't work for me when the script is called via sh nameofscript.sh, the name of the script is matched so mydir='nameofscript.sh'.
|
|
| Apr 1, 2016 at 0:06 | vote | accept | user3450548 | ||
| Mar 31, 2016 at 9:23 | comment | added | terdon♦ |
@Costas I edited to clarify that $0 is not always the absolute path. It is always the path to the script though, so this approach should work. Can you think of cases that would break it?
|
|
| Mar 31, 2016 at 9:22 | history | edited | terdon♦ | CC BY-SA 3.0 |
added 240 characters in body
|
| Mar 31, 2016 at 9:15 | comment | added | Costas |
~/bin/foo.sh it is just tilde expansion , not a full path. Use readlink -e $0 instead
|
|
| Mar 31, 2016 at 9:12 | history | answered | terdon♦ | CC BY-SA 3.0 |