Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • Note that the first one only work in bash, zsh or ksh. The later is about shell script, not awk script. Commented Sep 8, 2015 at 14:27
  • 2
    Thank you! ENVIRON["_"] works perfectly, and it doesn't call any external program. The second option awk -v ... depends on how one runs the script; I don't want this. Commented Sep 8, 2015 at 16:04
  • 2
    Calling your script tst.sh is misleading. It's an awk script, not a shell script. BEGIN is not a valid shell command. Commented Sep 9, 2015 at 19:45
  • 1
    Right but the portability question isn't "is ENVIRON[] portable" it's "does ENVIRON["_"] produce the calling shell script path when printed from every awk called via a shebang from every shell"? I would never call an awk script from a shebang to I personally don't care about the answer but just thought I'd mention it.... Oh I see in the comments above that @cuonglm answered that it's only supported in some shells. Commented Oct 10, 2017 at 18:08
  • 1
    Good point, @Ed. Verified as failing in dash (which returns the previous command (or else the shell itself) rather than the current one). ksh93 interestingly prefixes the PID in asterisks, e.g. *12345*/tmp/test.awk. ARGV[0] is reliably always awk in dash, bash, zsh, and ksh93. Commented Oct 11, 2017 at 15:43