what are some uses of .(single period) & ..(double period) in the shell command line

43,690

Solution 1

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 grep

Consider man cd, 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 sequence:

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 deleted. ...

and this, which is a step-by-step example of how the steps above seemingly apply to changing directory from /home/user to ../../boot/memtest86:

# echo $PWD
/home/user
(4). cd ../../boot/memtest86
(6). curpath = /home/user/../../boot/memtest86
(8)b 
/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 . and .. 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 bash

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 *,? and [...] matches itself. So this means a dot can match itself literally:

ls .*

...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 set and shopt).


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.

Solution 2

besides the use for working directory . also can be used as source, such as if your .bashrc changed, then you can use . .bashrc in terminal to make .bashrc take effect. it is the same as source .bashrc

Solution 3

I think it is important to point out that when you use either . to specify the current directory or .. to specify the parent directory, this is something which is generally interpreted by the underlying system calls used by the shell (or the program it runs) rather than by the shell itself. This is in contrast to using ~ which is expanded by the shell into the user's home directory when used at the start of a word.

There are several good answers here, so I won't copy them other than to say that of course the common case that is interpreted by the shell itself is to source a script in the same shell rather than running it in a separate process. In bash the builtin source is an alternative, however this does not exist in other POSIX shells so you are forced to use .

To add one of my own, you need to include the ./ at the start of a program or script that you want to run in the current directory (when the current directory is not in PATH). Eg ./myscript Although note that it is not so much the . as the presence of the / that causes this to work. If / is present the shell skips any attempt to locate in in PATH etc (except looking for an alias) and passes directly to the system for execution. The only reason for the . is so as not to have to specify the full path.

Solution 4

. runs a shell script in the current environment and then returns (see IBM's explanation here). If you run

. ./"yourscripthere"

The script will be run in your actual shell instead of spawning a new one. That way, any changes made by that script (variable, functions, etc...) will continue after you run it.

Solution 5

. is also used whenever you need to just specify the current directory. For instance, finding a file in current directory (and subdirectories) is how I call find 99% of the time:

find . -name '*.pdf'

or:

cp /some/file .

This dot could also be a double dot to search in one directory up.

So, what I'm trying to say, it's very common to specify directories like that, especially the current one.

Share:
43,690
Computernerd
Author by

Computernerd

I like to ask questions

Updated on September 18, 2022

Comments

  • Computernerd
    Computernerd almost 2 years

    The single period . means current working directory

    The double period .. means parent of the current working directory

    I can only think of . being used in relative pathname etc cd ./bin/usr

    How are the above . & .. used in the shell command line

    • slm
      slm over 10 years
      For more background I encourage you to read the computing section of Wikipedia on the topic: Dot_(Unix). The computing section
    • slm
      slm over 10 years
      Also take a look at this course material from a introductory Unix class: teaching.idallen.com/cst8207/13w/notes/…..
    • Justsalt
      Justsalt over 8 years
      Two Unix gurus are arguing about how many unique uses of the period there are in Unix. One carefully counts them up in his mind and announces there are 22. The other is sure he can think of 23. The first challenges the second to name all 23 uses. He begins, "1. In a file of English language text a period can be used to end a sentence." The first guru slaps his forehead and says, "You're right. There are 23."
  • S edwards
    S edwards over 10 years
    Nice hint, I never though about it that's a nice idea
  • Stéphane Chazelas
    Stéphane Chazelas over 10 years
    An exception is for cd/pushd in csh and POSIX shells which have their own interpretation of .. and don't rely on the system to expand it (except with -P).
  • Walter Tross
    Walter Tross over 5 years
    the other way to achieve the same result is stat -- -x