Antares
Antares

Reputation: 235

What is the meaning of `cd =-`?

As usual, I mistyped the command

[csh]$ cd -

and typed

[csh]% cd =-

Surprisingly, this worked!

Question: What does the meaning of the equal-sign (=) here?

Upvotes: 1

Views: 192

Answers (1)

Antares
Antares

Reputation: 235

I think it is related to directory stack, which is usually manipulated by pushd and popd.

In csh, cd =(index) pulls out the element of that index from the directory stack, and replace the top element (index 0), which is also the current directory.

- as an index here is for the bottom element in the directory stack. Or, - is for the previous directory if the directory stack contains only the current directory, which means you popd out all the directories or you are not using pushd at all. In this case, cd =- works just the same as cd -.

Here is the explanation from man page:

   Directory stack substitution (+)
   The  directory stack is a list of directories, numbered from zero, used by the pushd, popd and dirs builtin commands (q.v.).  dirs can
   print, store in a file, restore and clear the directory stack at any time, and the savedirs and dirsfile shell variables can be set to
   store  the  directory  stack  automatically on logout and restore it on login.  The dirstack shell variable can be examined to see the
   directory stack and set to put arbitrary directories into the directory stack.

   The character ‘=’ followed by one or more digits expands to an entry in the directory stack.  The special case  ‘=-’  expands  to  the
   last directory in the stack.  For example,

       > dirs -v
       0       /usr/bin
       1       /usr/spool/uucp
       2       /usr/accts/sys
       > echo =1
       /usr/spool/uucp
       > echo =0/calendar
       /usr/bin/calendar
       > echo =-
       /usr/accts/sys

   The  noglob  and  nonomatch  shell variables and the expand-glob editor command apply to directory stack as well as filename substitu-
   tions.

Upvotes: 2

Related Questions