What Does 'Cd -' Stand For

What does 'cd -' stand for?

If a single dash is specified as the argument, it will be replaced by the value of OLDPWD.

The OLDPWD is set by cd command and it is the previous working directory.

What does the 'cd' in a database column stand for?

It's a simple_enum table suffix that according to the author simply stands for code.

What is the point of cd . in cd . && npm install?

It means that you ran this:

$ express

(or possibly express .)

In other words, you told express-generator to create a project in the current working directory. Alternatively, you could have provided a subdirectory name:

$ express foo

In that case, the commandline it generates makes a bit more sense:

install dependencies:
$ cd foo && npm install

what does 'cd $_' mean?

$_ expands to the last argument to the previous simple command* or to previous command if it had no arguments.

mkdir my-new-project && cd $_

^ Here you have a command made of two simple commands. The last argument to the first one is my-new-project so $_ in the second simple command will expand to my-new-project.

To give another example:

echo a b; echo $_
#Will output:
#a b
#b

In any case, mkdir some_dir && cd $_ is a very common combo. If you get tired of typing it, I think it's a good idea to make it a function:

mkdircd() {
#Make path for each argument and cd into the last path
mkdir -p "$@" && cd "$_"
}

*
The bash manual defines a simple command as "a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. " where control operator refers to one of || & && ; ;; ( ) | |& <newline>.

In practice$_ works like I've described but only with ||, &&, ;, or newline as the control operator.

What does cd.. mean in powershell?

.. in filesystem paths represents a given path's parent path.

Without an explicit path preceding the .., the implied path is the current [working] directory, so the .. therefore refers to the current directory's parent directory.

In short: cd with an argument of .. changes to the current directory's parent directory, i.e., the directory one level above in the directory hierarchy.

Shell-specific use of .. with cd:

  • The legacy command processor, cmd.exe ("Command Prompt") - seemingly with the internal cd command specifically (see Mofi's comments on the question) - considers an . character to implicitly start the cd command's argument.

    • Therefore, separating the cd command from the .. argument with a space character, as usual, isn't necessary; that is, instead of cd .. you can type cd.., which is a shortcut that users of cmd.exe have grown accustomed to over the years.
  • PowerShell allows . characters to be used in command names, so submitting cd.. does not invoke the cd command (a built-in alias for the Set-Location cmdlet) with argument .. - instead, it looks for a command literally named cd..

    • To accommodate cmd.exe users who are accustomed to the cd.. shortcut, PowerShell comes with a parameter-less function literally named cd.., so that submitting cd.. as a command in PowerShell effectively works the same as in cmd.exe.

      • However, note that only cd.. works, not also specifying additional components; that is, something like cd..\Foo (which works in cmd.exe) fails.
    • Get-Command cd.. | Format-List shows information about this function, which reveals that it simply calls Set-Location .., which is PowerShell's equivalent of cmd.exe's cd ..

What is the `Cd` command?

macOS uses a case-insensitive filesystem by default[1]
, which can be misleading at times:

which Cd is effectively the same as which cd and which CD in terms of returning the (effectively) same file path.

Confusingly, even though all 3 command refer to the same file, they do so in a case-preserving manner, misleadingly suggesting that the actual case of the filename is whatever you specified.

As a workaround, you can see the true case of the filename if you employ globbing (filename expansion):

$ ls "$(which Cd)"*  # could match additional files, but the one of interest is among them
/usr/bin/cd # true case of the filename

Bash (the macOS default shell) is internally case-sensitive.
That is, it recognizes cd as builtin cd (its built-in directory-changing command).

By contrast, it does NOT recognize Cd as that, due to the difference in case.

Given that it doesn't recognize Cd as a builtin, it goes looking for an external utility (in the $PATH), and that is when it finds /usr/bin/cd.


/usr/bin/cd is implemented as a shell script, which is mostly useless, because as an external utility it cannot affect the shell's state, so its attempts to change the directory are simply quietly ignored.

(Keith Thompson points out in a comment that you can use it as test whether a given directory can be changed to, because the script's exit code will reflect that).

Matt's answer provides history behind the inclusion of the script in FreeBSD and OSX (which mostly builds on FreeBSD), but it's worth taking a closer look at the rationale (emphasis mine):

From the POSIX spec:

However, all of the standard utilities, including the regular built-ins in the table, but not the special built-ins described in Special Built-In Utilities, shall be implemented in a manner so that they can be accessed via the exec family of functions as defined in the System Interfaces volume of POSIX.1-2008 and can be invoked directly by those standard utilities that require it (env, find, nice, nohup, time, xargs).

In essence, the above means: regular built-ins must (also) be callable stand-alone, as executables (whether as scripts or binaries), nut just as built-ins from within the shell.

The cited regular built-ins table comprises these utilities:

alias bg cd command false fc fg getopts jobs kill newgrp pwd read true umask unalias wait

Note: special built-in utilities are by definition shell-internal only, and their behavior differs from regular built-in utilities.

As such, to be formally POSIX-compliant an OS must indeed provide cd as an external utility.

At the same time, the POSIX spec. does have awareness that at least some of these regular built-ins - notably cd - only makes sense as a built-in:

"Since cd affects the current shell execution environment, it is always provided as a shell regular built-in." - http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cd.html

Among the regular built-in utilities listed, some make sense both as a built-in and as an external utility:

For instance kill needs to be a built-in in order to kill jobs (which are a shell-internal concept), but it is also useful as an external utility, so as to kill processes by PID.

However, among the regular built-in utilities listed, the following never make sense as external utilities, as far as I can tell Do tell me if you disagree
, even though POSIX mandates their presence
:

alias bg cd command fc fg getopts jobs read umask unalias

Tip of the hat to Matt for helping to complete the list; he also points that the hash built-in, even though it's not a POSIX utility, also has a pointless script implementation.


[1] As Dave Newton points out in a comment, it is possible to format HFS+, the macOS filesystem, in a case-sensitive manner (even though most people stick with the case-insensitive default). Based on the answer Dave links to, the following command will tell you whether your macOS filesystem is case-insensitive or not:

diskutil info / | grep -iq '^\s*Name.*case-sensitive*' && echo "case-SENSITIVE" || echo "case-INsensitive"

Why does cd - behave as it does?

cd - 

pop the last directory you were from the stack of directory. It's like hitting "back" on the browser.

Exemple :

you are in /user/alex

you can test that with :
pwd
that give you
/user/alex

then if you do

%cd project1/subfolder
%pwd
/user/alex/project1/subfolder
%cd subsubfolder
%pwd
/user/alex/project1/subfolder/subsubfolder
%cd -
pwd
/user/alex/project1/subfolder
cd -
pwd
/user/alex

NB : it's not going back a level upper in the folder hierarchy. it's going to the previous current folder. ( a level upper is cd .. ).



Related Topics



Leave a reply



Submit