What is the difference between the cd
shell command and the Perl function chdir
? Please can you explain with an example?
The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.
chdir changes the current working directory of the calling process to the directory specified in path. Syntax: int chdir(const char *path); Parameter: Here, the path is the Directory path which the user want to make the current working directory.
cd command in linux known as change directory command. It is used to change current working directory. Syntax: $ cd [directory] To move inside a subdirectory : to move inside a subdirectory in linux we use $ cd [directory_name]
If successful, chdir() changes the working directory and returns 0. If unsuccessful, chdir() does not change the working directory, returns -1, and sets errno to one of the following values: Error Code.
Essentially both of them do the same thing, but chdir
is a POSIX system call while cd
is a normal function used in a program which in this case is the shell.
In practice, chdir
is called by cd
to make the change in directory since the program does not have kernel privileges to do it by itself.
The cd
command changes the current directory of a shell process; the Perl chdir
function changes the current directory of a Perl process. They're exactly the same thing, just spelled differently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With