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 why not just $pwd? :P
@nakdim @nixCraft in a directory called '/bin/rm *' 🤪 I dare you!
@nakdim @nixCraft Because it will try to execute the value of $pwd. Which in some shells will try to go to that path, thus doing nothing because you're already there, and in others will fail because it isn't a valid command (I believe, haven't tested that)
@nixCraft I mean they all work, but 'echo "$PWD"' is the most direct and takes the least typing.
@ellestad
Wouldn't you rather omit the doublequotes?
@nixCraft
@ellestad @nixCraft pwd is a command that works everywhere I've tried it. Works at least in bash and ash.
@nixCraft echo for me, but how about
>env |grep PWD ?
@nixCraft sometimes I use $PWD<TAB>
@nixCraft I took the awk solution just becaue I love awk.
@nixCraft
I like that first one. Gratuitous use of `awk` is always welcome.
@nixCraft `readlink /proc/self/cwd`
@nixCraft not sure why you wrote "choose one answer only". That's how radio buttons work.

@nixCraft

Voted for printf, just to gratify my inner C snob (that doesn't actually know much C at all ^__^)
But I *do* use it occasionally in scripts and one-liners when I need nicely formatted output.

@nixCraft
The second one only works if you use bash, so not recommended in scripts. Or if, like me, you use tcsh.
@nixCraft Clearly python3 -c 'import os; print(os.getenv("PWD"))' is the best option.
@nixCraft awk, awk, awk!
he’s good with text, he’s good with lines, he’s good with columns… he’s perfect! 
@nixCraft just use pwd -L as it is available on BSD, AIX, SCO and the great majority of GNU/Linux distributions.
Otherwise you could use perl 😉 perl -e 'use Cwd; print getcwd . "\n";'
@nixCraft Usually I just do
f=$(mktemp) ; printf '#include <stdio.h>\n#include <stdlib.h>\nint main() { printf("%%s\\n", getenv("PWD")); }' > $f.c ; cc $f.c ; ./a.out
@nixCraft Which way is the correct way?

@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

@nixCraft

echo $env:PWD

because PowerShell

which is a shell too.

@nixCraft wait is this a trick question? Where did just typing `pwd` go

@nixCraft None of the above.

echo ${PWD}

@nixCraft hmm. Never thought of echoing it. I always use the pwd command.
If needed, I might send its output to a file.
@nixCraft the awk route just cause screw readability
@nixCraft echo in a shell but voted printf because in scripts and coding it's often tremendously elegant
@nixCraft Honestly, TIL $PWD is even a thing.
@nixCraft I selected the awk command, but as a linux genius, I would have passed that awk command into a perl script.

@nixCraft

echo "PWD" | python3 -c 'import subprocess,sys; var = sys.stdin.read().strip(); print(subprocess.run(["awk", "BEGIN {print ENVIRON[\"" + var + "\"]}"], stdout=subprocess.PIPE).stdout.decode("UTF-8"))'