1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00

Add List The PID And Name Of Current Shell Process as a Unix TIL

This commit is contained in:
jbranchaud
2025-02-23 10:12:52 -06:00
parent f9b966a0f1
commit 84548b7a7f
2 changed files with 29 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1598 TILs and counting..._
_1599 TILs and counting..._
See some of the other learning resources I work on:
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
@@ -1575,6 +1575,7 @@ See some of the other learning resources I work on:
- [List Parent pid With ps](unix/list-parent-pid-with-ps.md)
- [List Stats For A File](unix/list-stats-for-a-file.md)
- [List The Available JDKs](unix/list-the-available-jdks.md)
- [List The PID And Name Of Current Shell Process](unix/list-the-pid-and-name-of-current-shell-process.md)
- [List The Stack Of Remembered Directories](unix/list-the-stack-of-remembered-directories.md)
- [List TXT DNS Records For A Domain](unix/list-txt-dns-records-for-a-domain.md)
- [Load Env Vars In Bash Script](unix/load-env-vars-in-bash-script.md)

View File

@@ -0,0 +1,27 @@
# List PID And Name Of Current Shell Process
In Julia Evans' [How to add a directory to your
PATH](https://jvns.ca/blog/2025/02/13/how-to-add-a-directory-to-your-path/),
she shows off an odd-looking command for determining what shell (e.g. bash or
zsh) you are currently running.
```bash
$ ps -p $$ -o pid=,comm=
38105 -zsh
```
I already know I'm running `zsh`, but I thought this command was interesting
enough to dig into and break down.
- The `ps` command lists processes that "have controlling terminals"
- The `$$` is a special shell variable representing the PID of the current process (try `echo $$`)
- The `-p` flag allows you to specify a PID for `ps` to grab, in this case, the `$$` PID
- The `-o` flag allows us to specify the output format, such as the PID and command name
- The `=` after `pid` and `comm` tell `ps` to exclude headers from the output
Additionally, I noticed that it output `-zsh` (not just `zsh`). That leading
hyphen seems to indicate that [this `zsh` process is a _login
shell_](https://unix.stackexchange.com/a/46856/5916). That means it was the
process used to initiate an interactive shell session and something like the
`.zprofile` would have been sourced as part of that.