How are the above . & .. used in the shell command line?
In many ways depending on context. Simple illustrations:
- For path resolution with the builtin
cd command for example like you said
Literally, inside a pattern, for instance in the context of pathname expansion
- In a
pattern to replace any single one character with a command like
man path_resolution and this explanation on how the builtin
cd command works internally (
man cd here):
(4). If the first component of the directory operand is dot or dot-dot, proceed to step 6.
(6). Set curpath to the string formed by the concatenation of the
value of PWD, a slash character, and the operand.
(8). The curpath value shall then be converted to canonical form as
follows, considering each component from beginning to end, in
a. Dot components and any slashes that separate them from the next
component shall be deleted.
b. For each dot-dot component, if there is a preceding component
and it is neither root nor dot-dot, the preceding component, all
slashes separating the preceding component from dot-dot, dot-dot and
all slashes separating dot-dot from the following component shall be
and this, which is a step-by-step example of how the steps above seemingly apply to changing directory from
# echo $PWD
(4). cd ../../boot/memtest86
(6). curpath = /home/user/../../boot/memtest86
/home/../boot/memtest86 //step 1
/ boot/memtest86 //step 2
curpath = /boot/memtest86 //result
By trimming dot components to the same extent as the working directory depth, inevitably this yields a path expressed from the root. The steps for a single dot case etc. were skipped since it concatenates like above, then removes
./ which is very easy to imagine as we're in the current directory. So this illustrates how
.. referencing the current and parent directory is implemented in this command's context internally, which is not through "substitution" but rather something akin to stream editing...
You can investigate pathname expansion and pattern matching in
After word splitting, unless the -f option has been set (see The Set
Builtin), Bash scans each word for the characters *, ?, and [...]
If one of these characters appears, then the word is regarded as a
pattern, and replaced with an alphabetically sorted list of file names
matching the pattern. ...
In a pattern for a pathname like this, any character other than
[...] matches itself. So this means a dot can match itself literally:
...will expand to
. (because * includes also null) and accordingly will list current and parent directory and also any file or directory in the current directory which literally starts with a dot (.*) and is followed by something else of whatever length. As explained in the manual you have some level of control over the expansion behavior etc. (using *glob options to
Here is an example of the character being used in a pattern (as it's
grep pattern file1) to select any one character, and is of course unrelated to file or directory names:
ls -la | grep .
This prints the directory contents so it catches everything (
grep -v . is used to catch empty lines). Generally with patterns in such expressions and most certainly that is the case with
grep, a single dot matches any single character. Finally consider these special characters examples in the dot
. section for a quick reminder of some of the different use cases.
So a dot can be used in path resolution, as a literal character which is part of a pattern, it can replace a single character in an expression with
grep and it can be even be a bash builtin used to execute commands from a file etc. How it is used on the command line varies with context...
1. This is of course about the syntax of a specific command. If a command accepts a file/path as an argument then using the dot in its stead, as others have shown, expands to the current working directory which is quite convenient.