Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding variable containing current shell pid #1202

Closed
bstarynk opened this issue Dec 24, 2013 · 14 comments
Closed

adding variable containing current shell pid #1202

bstarynk opened this issue Dec 24, 2013 · 14 comments

Comments

@bstarynk
Copy link

@bstarynk bstarynk commented Dec 24, 2013

Hello all,

I am missing the $$ special variable to denote the current shell's pid (see http://hyperpolyglot.org/unix-shells giving a nice table ...); has it been discussed and rejected? What files should I look in to consider adding it?

@zanchey
Copy link
Member

@zanchey zanchey commented Dec 24, 2013

You are looking for the %self process expansion - see http://fishshell.com/docs/current/index.html#expand-process .

@xfix
Copy link
Member

@xfix xfix commented Dec 24, 2013

Perhaps it could be possible to mention %self when user types $$ followed by non-letter.

@bstarynk
Copy link
Author

@bstarynk bstarynk commented Jan 8, 2014

The point is that $$ is very common in most other shells. So I believe that it should be expanded like %self is. Using $$ lowers the fish learning effort.

@maxfl
Copy link
Contributor

@maxfl maxfl commented Jan 8, 2014

One of the problems with %self is that it is not expanded in a string. You can not create file file.%self
I dislike $$, but it has an advantage of expanding in this case.

@cljacobs
Copy link

@cljacobs cljacobs commented Jan 17, 2014

You can create such a file using touch file.(echo %self). It's a lot more typing, and not as intuitive to people moving from other shells, but I disagree that $$ should be expandable to the shell's pid for the same reasons fish ignores convention for many of those other special-case variable expansions other shells have adopted.

I don't really use %self very much, and even less in scenarios where it would be difficult to implement without a construct like $$, but if it's something people do a lot and given that it's already been given a special recognized syntax in process expansion, perhaps a variable expansion a la $argv would suffice, e.g. $pid or $self? This would violate the law of orthogonality, but possibly satisfies the law of user focus much better than the current way of doing things.

@zanchey
Copy link
Member

@zanchey zanchey commented Oct 8, 2015

The lack of expansion in a string is #1074.

@krader1961
Copy link
Contributor

@krader1961 krader1961 commented Mar 24, 2016

I'm in agreement with @cljacobs. I don't think we should introduce $$ but introducing $PID or $SELF can be justified despite introducing a second way to do the same thing. Note the all uppercase names to mirror other vars like $PWD and minimize clashes with existing uses of their lowercase forms.

@ridiculousfish
Copy link
Member

@ridiculousfish ridiculousfish commented Mar 24, 2016

It's also very easy for any user to just write set SELF %self at the top of the script and then they have it. This wouldn't work in bash since bash forks for subprocesses, but it works in fish.

@krader1961
Copy link
Contributor

@krader1961 krader1961 commented Mar 24, 2016

That set SELF %self works actually surprises me. As does touch %self but not touch x.%self.

@kgadek
Copy link

@kgadek kgadek commented Jul 29, 2016

I would add that using $$ is very common to detect the shell type itself. I use this to do that: ps -cp $$ | awk 'NR==2{sub(/^-?/, "", $NF); print $NF}' and it works quite nicely across macOS, Linux and FreeBSD and various shells… except fish.

@krader1961 krader1961 modified the milestone: fish-tank Nov 17, 2016
@krader1961 krader1961 added the RFC label Nov 17, 2016
@krader1961 krader1961 added this to the fish-future milestone Jun 21, 2017
@krader1961 krader1961 removed the RFC label Jun 21, 2017
@krader1961
Copy link
Contributor

@krader1961 krader1961 commented Jun 21, 2017

I'm removing the "RFC" label. This should be implemented. I have no objections to using %some-string to match process names containing that string. But given the quirks of %self (e.g., touch x.%self doesn't work) we should allow access to this info via normal variable expansion.

@faho
Copy link
Member

@faho faho commented Jun 21, 2017

But given the quirks of %self (e.g., touch x.%self doesn't work) we should allow access to this info via normal variable expansion.

Okay. What should the variable be called? Note that $ is currently not a valid variable name. It's also not in any way explanatory, so I'm leaning towards using a more descriptive name. Some suggestions:

  • $SELF
  • $PID
  • $FISH_PID
  • $SHELL_PID

I like the latter two the most since they best explain what the variable is.

@xfix
Copy link
Member

@xfix xfix commented Jun 21, 2017

I think the name should be lowercase, considering shell PID is not an exported variable (for consistency).

@mqudsi mqudsi closed this in 0866653 Mar 9, 2018
@faho faho changed the title adding $$ support (for current shell pid) in fish adding variable containing current shell pid Sep 30, 2018
@faho faho modified the milestones: fish-future, fish-3.0 Sep 30, 2018
@cben
Copy link
Contributor

@cben cben commented Oct 25, 2018

For the historic record, the new vars got called $fish_pid (for %self) and $last_pid (replacing %last).

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
10 participants