mastodon.me.uk is one of the many independent Mastodon servers you can use to participate in the fediverse.
Open, user-supported, corporation-free social media for the UK.

Administered by:

Server stats:

486
active users

The commands 'cd $PWD' and 'cd .' in bash both look like useless no-ops which change into the same directory you're already in. But they're not, and both can be useful.

$PWD is a string variable which caches the pathname that your current directory had at the time you changed into it. It's not automatically updated in between cd commands.

So 'cd $PWD' (or 'cd "$PWD"' if you're being properly careful) changes to whatever directory _now_ lives at the pathname that your actual cwd _was_ when you changed to it.

'cd .' really _does_ just change directory to the same physical directory you're already in, but it's still not useless, because it causes bash to recalculate the value of $PWD.

For example, if one shell is in ~/test, and in another shell you rename ~/test to ~/newname and make a new directory ~/test, then in the first shell 'cd $PWD' will move to the new ~/test, whereas 'cd .' will stay in the original directory but update $PWD to reflect the fact that it now lives at ~/newname.

Ian Jackson

@simontatham I often use cd `pwd` which (in bash at least) is pretty close to the effect of cd $PWD, but easier to type.

This is useful with tests that blow away and recreate a temporary workspace. You run the test, and in another shell cd there and inspect its files etc. The next iteration will delete your inspection shell's cwd - but cd `pwd` puts you in the same place in the new run's output.

For this, the test case must reuse the same temp path. But you wanted to avoid /tmp, anyway.