The built-in cd command in the POSIX shell sh makes the specified directory your current working directory.
The cd command is rejected in a restricted shell.
Syntax
Format 1: |
cd[ directory] |
Format 2: |
cd -
|
Format 3: |
cd old new |
Change directory using CDPATH directory Name of the directory that is to become your current working directory. You must have If the name of the specified directory begins with one of the following characters, the
If the name of the specified directory does not begin with any of the above characters, cd evaluates the CDPATH environment variable:
directory not specified:
Specifying the - as an operand has the same effect as the command
Change directory with text substitution cd substitutes the string new for the string old in the current directory name (PWD) and tries to change to this new directory. |
Error
file:
cd has been rejected because you are working in a restricted shell. |
Variable
HOME contains the absolute path name of your home directory. CDPATH You can assign to CDPATH the absolute path names of directories that cd is to search. OLDPWD Path name of the previous directory used by cd -. PWD Path name of the current directory. This name is set by cd following the change to this directory. |
Locale
The following environment variables affect the execution of cd: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments). LC_CTYPE governs character classes, character conversion (shifting) and the behavior of character classes in regular expressions. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
The following entry makes the subdirectory dates the current directory:
|
Example 2
User rose has redefined the CDPATH environment variable. She now wishes to change to her subdirectory usr, but with the following commands ends up in the directory /usr instead:
The usr directory is first looked for in the directories whose path names are assigned to the CDPATH variable. In this case, CDPATH contains a / for the root directory as the first path name. The current directory is the last to be searched by cd. User rose can prevent cd from evaluating the CDPATH environment variable by formulating the command in the following way:
|
See also
pwd chdir() [4] |