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. – Lie Ryan Jan 22 '19 at 06:46cd .
is useful, and whether there's more to it than just face-value navigation to same directory. You can convert your comment into an answer, as it may be useful to others. – Sergiy Kolodyazhnyy Jan 22 '19 at 07:21$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… – Andreas Wiese Jan 22 '19 at 22:44cd .
, 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. – 3D1T0R Jan 23 '19 at 00:47cd
is a shell built-in command. What shell do you ask about? – enkryptor Jan 24 '19 at 15:32/bin/cd
and IIRC is required by POSIX. There's no specific restriction to which shell this question applies, and I as shown in linked question and some of the already existing answers - behavior ofcd .
can vary by shell, so addressing multiple cases is necessary. – Sergiy Kolodyazhnyy Jan 25 '19 at 00:07/bin/cd
": Wait, how does that work? Are there Unixlike systems where an executable program is allowed to mess with the parent process's execution environment? – ruakh Jan 25 '19 at 06:56/bin/cd
here https://unix.stackexchange.com/q/50058/85039 – Sergiy Kolodyazhnyy Jan 25 '19 at 07:18chdir
that did affect shell's execution environment, according to Dennis Ritchie's article The Evolution of the Unix Time-sharing System*. According to it, when they realized that it stopped working with newly addedfork()
syscall, they decided to make it a shell built-in, or so I understand. Eventuallychdir
was dropped, apparently in Version 7 (source) – Sergiy Kolodyazhnyy Jan 25 '19 at 07:35cd .
to cause the plugin to display the correct branch. – Jared Smith Jan 26 '19 at 00:33cd -P .
almost every day, does that count? When you've cd'ed somewhere through symlinks, that can be very useful to figure out where you really are. Is that relevant or are you only asking aboutcd .
with no flags? – terdon Jan 28 '19 at 11:46