Note that "." is the proper way to specify the name of the file that is open as the current working directory of any process (including a shell process of course), and "." is always a valid name of a file in any and all directories, including the current working directory. The name .
may not be a valid name for a file for a given instance of a process if, say, the underlying current working directory has been removed (or gone "bad", e.g. a stale NFS handle), but it is a valid name of a file that is guaranteed to exist in every valid directory.
So .
must be a valid argument for any command that accepts the name of a directory, and thus in the standard shell cd .
must be a valid command.
Whether cd .
is useful or not depends on the shell implementation. As mentioned it can be useful if the shell resets its internal idea of the full pathname of the current working directory after calling the underlying chdir
system call, say for example if the underlying directory (or some parent of it) has been renamed.
At least some shells I know (/bin/sh
on FreeBSD and NetBSD) will convert cd ""
into cd .
, which can arguably be described a feature to support programmatic use in a shell script where a variable might be used as a parameter (i.e. converting an empty variable substitution into a "do nothing" result), though the FreeBSD commit history says the change was directly due to adding POSIX support to prevent a failure from chdir("")
, which POSIX mandates must fail.
Some other shells will replace the .
with whatever they have stored as the fully qualified pathname to their current working directory, and thus for them this may allow for the behaviour mentioned in Sahil Agarwal's answer.
cd .
to trigger those checks because it's short and simple. Though I think you intended the question to be for a vanilla environment.$PWD
,cd .
also changes$OLDPWD
to the current directory. I have (currently) no idea why this might be useful, but for the sake of completeness…cd .
, though seeing the answers below, I might in the future, but I have on occasion usedpushd .
when I wanted to be able topopd
back to this directory later. e.g. when running a build script that doesconfigure
,cd output...
andmake
, and when it's done I'll want to go back to the original directory. Rather than maintaining my own copy of the buildscript that's different than what everyone else expects, I just run it aspushd .; ./BuildScriptName.sh; popd
, and this also gives me the freedom to notpopd
sometimes, and thenpopd
later instead./bin/cd
here unix.stackexchange.com/q/50058/85039