This question already has an answer here:

Suppose one executes either

. some_script


source some_script

How can some_script determine the absolute path of the current shell (i.e. the one that is executing the . or source command)? (Note that, in general, this path is not the same as the value of $SHELL.)

In other words, I'm looking for the "absolute path" counterpart of $$. I figure that the current shell process must know the executable whence it came, but AFAICT, it's not stored in any environment variable.

The best I can come up with boils down to scraping the output generated by a suitable variant of ps -p $$; for example:

CURRENT_SHELL=${"$( PS_FORMAT=cmd PS_PERSONALITY=linux ps -p $$ -h )"% *}

This is probably not very portable. (And it's ugly, to boot.)

I'm primarily interested in the case where the current shell is some version of zsh. (I have a secondary interest in the corresponding answer for bash.)

marked as duplicate by kjo, Jeff Schaller, MelBurslan, Jakuje, don_crissti Mar 31 '16 at 22:32

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.

  • Not sure, but does readlink /proc/$$/exe get what you want? – Eric Renouf Mar 31 '16 at 15:14
  • 5
  • @dave_thompson_085: most embarrassing... it's my own question. I knew I had asked something like it before, but I did not find it, probably because in all my searches I was using the word "source"... – kjo Mar 31 '16 at 21:19

Here's what I use:

head -n 1 /proc/$$/status | cut -f2

I'm not sure it's portable to all Unices, but it works in Linux and Cygwin.

Not the answer you're looking for? Browse other questions tagged or ask your own question.