Quiz: How do you print the value of the $PWD shell variable in #Linux or #Unix? Choose one answer only.
awk 'BEGIN {print ENVIRON["PWD"]}'
3%
cat <<<"$PWD"
1.4%
echo "$PWD"
92.8%
printf "%s\n" "$PWD"
2.8%
Poll ended at .

@nixCraft

cat /proc/cwd

You can not trust ENVIRONMENT

There are issues with getenv(), putenv(), and setenv(). The semantics are not consistent and can be abused.

@SpaceLifeForm @nixCraft hang on for real? And here I thought I was being silly when the obvious answer is `pwd`

@fl0und3r @nixCraft

The pwd executable inherients the ENVIRONMEMT via fork() and execve() from the parent process.

If the enviroment variables have been corrupted already, then there could be turtles.

Most code does not call chdir() so using PWD probably did not change.

But, if the software in question creates addiitonal environment variables that point to subdirectories, there could be issues, especially if there are libraries involved that have not been vetted via source code review.

@SpaceLifeForm @nixCraft I'm having a hard time convincing `getcwd` that I'm somewhere I'm not by messing with the environ. presumably this is some deep magic beyond my (limited) understanding? I guess my takeaway is to trust `getcwd` and not `getenv`?

@fl0und3r @nixCraft

What platform? What libc?

@SpaceLifeForm @nixCraft `Ubuntu GLIBC 2.35-0ubuntu3.8`
linux mint running Kernel 6.8.0-45-generic x86_64