Bash script get current file name




















Codebeginner Codebeginner 13 1 1 silver badge 3 3 bronze badges. Could you please edit your question to show some examples of inputs and the desired outcome, along with what you've tried and what about it didn't work the way you wanted it to? Add a comment. Active Oldest Votes. Improve this answer. Kamaraj Kamaraj 3, 1 1 gold badge 10 10 silver badges 17 17 bronze badges. It did not work.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

For any other feedbacks or questions you can either use the comments section or contact me form. So it appears there is no generic Unix shell method of getting the dir D in which a script S resides, when invoked as :. Save my name and email in this browser for the next time I comment.

Notify me via e-mail if anyone answers my comment. Table of Contents. Related Posts. The bottom-most element the one with the highest index is "main". This variable exists only when a shell function is executing.

The caller builtin displays the current call stack using this information. These answers are correct for the cases they state but there is a still a problem if you run the script from another script using the 'source' keyword so that it runs in the same shell. And in this case, I don't think it is possible to get the name of the script itself.

This is an edge case and should not be taken TOO seriously. Since some comments asked about the filename without extension, here's an example how to accomplish that:. I agree that it would be confusing to dereference symlinks if your objective is to provide feedback to the user, but there are occasions when you do need to get the canonical name to a script or other file, and this is the best way, imo. This resolves symbolic links realpath does that , handles spaces double quotes do this , and will find the current script name even when sourced.

This works fine with. Here is what I came up with, inspired by Dimitre Radoulov 's answer which I upvoted, by the way. I got the above from another Stack Overflow question, Can a Bash script tell what directory it's stored in? Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How do I know the script file name in a Bash script?

Ask Question. Asked 13 years, 3 months ago. Active 5 months ago. Viewed k times. How can I determine the name of the Bash script file inside the script itself? Peter Mortensen Ma99uS Ma99uS 9, 7 7 gold badges 30 30 silver badges 42 42 bronze badges. Similar [Can a bash script tell what directory its stored in? For directory, see: Getting the source directory of a Bash script from within. Add a comment. Active Oldest Votes.

BeeOnRope Tanktalus Tanktalus It works unless you're being called via symlink. But, even then, it's usually what you want anyway, IME. Doesn't work for scripts which are sourced as opposed to invoked. Charles Duffy: See Dimitre Radoulov's answer below.



0コメント

  • 1000 / 1000