What is the difference between the commands builtin cd and cd?












10















I encountered a Linux command, builtin cd.



What is the difference between the commands builtin cd and cd?



In fact, I made some researches about the difference, but I could not find a remarkable and significant explanation about this.










share|improve this question




















  • 1





    See also unix.stackexchange.com/q/38808/117549

    – Jeff Schaller
    7 hours ago











  • See also: stackoverflow.com/q/15691977/974555

    – gerrit
    27 mins ago
















10















I encountered a Linux command, builtin cd.



What is the difference between the commands builtin cd and cd?



In fact, I made some researches about the difference, but I could not find a remarkable and significant explanation about this.










share|improve this question




















  • 1





    See also unix.stackexchange.com/q/38808/117549

    – Jeff Schaller
    7 hours ago











  • See also: stackoverflow.com/q/15691977/974555

    – gerrit
    27 mins ago














10












10








10


2






I encountered a Linux command, builtin cd.



What is the difference between the commands builtin cd and cd?



In fact, I made some researches about the difference, but I could not find a remarkable and significant explanation about this.










share|improve this question
















I encountered a Linux command, builtin cd.



What is the difference between the commands builtin cd and cd?



In fact, I made some researches about the difference, but I could not find a remarkable and significant explanation about this.







bash shell cd-command






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 5 hours ago









Peter Mortensen

89558




89558










asked 8 hours ago









GoktugGoktug

2268




2268








  • 1





    See also unix.stackexchange.com/q/38808/117549

    – Jeff Schaller
    7 hours ago











  • See also: stackoverflow.com/q/15691977/974555

    – gerrit
    27 mins ago














  • 1





    See also unix.stackexchange.com/q/38808/117549

    – Jeff Schaller
    7 hours ago











  • See also: stackoverflow.com/q/15691977/974555

    – gerrit
    27 mins ago








1




1





See also unix.stackexchange.com/q/38808/117549

– Jeff Schaller
7 hours ago





See also unix.stackexchange.com/q/38808/117549

– Jeff Schaller
7 hours ago













See also: stackoverflow.com/q/15691977/974555

– gerrit
27 mins ago





See also: stackoverflow.com/q/15691977/974555

– gerrit
27 mins ago










3 Answers
3






active

oldest

votes


















20














The cd command is a built-in, so normally builtin cd will do the same thing as cd. But there is a difference if cd is redefined as a function or alias, in which case cd will call the function/alias but builtin cd will still change the directory (in other words, will keep the built-in accessible even if clobbered by a function.)



For example:





user:~$ cd () { echo "I won't let you change directories"; }
user:~$ cd mysubdir
I won't let you change directories
user:~$ builtin cd mysubdir
user:~/mysubdir$ unset -f cd # undefine function


Or with an alias:



user:~$ alias cd='echo Trying to cd to'
user:~$ cd mysubdir
Trying to cd to mysubdir
user:~$ builtin cd mysubdir
user:~/mysubdir$ unalias cd # undefine alias


Using builtin is also a good way to define a cd function that does something and changes directory (since calling cd from it would just keep calling the function again in an endless recursion.)



For example:



user:~ $ cd () { echo "Changing directory to ${1-home}"; builtin cd ${1+"$1"}; }
user:~ $ cd mysubdir
Changing directory to mysubdir
user:~/mysubdir $ cd
Changing directory to home
user:~ $ unset -f cd # undefine function





share|improve this answer





















  • 1





    +1 The examples are particularly illustrative here.

    – Tashus
    2 hours ago






  • 1





    In the case of an alias, is there any difference between builtin cd mysubdir and cd mysubdir?

    – gerrit
    26 mins ago



















10














In most instances, there is no difference (but see below). The cd command is a built-in command in all shells. It needs to be built-in1 as an external command can not change the environment of the invoking shell, and changing the working directory constitutes a change in its environment.



The bash command builtin forces the shell to use the built-in version of a command, even though there may be a shell function, alias, or external command available with the same name.



In the case where there is e.g. a shell function with the name cd, then builtin cd would not call that. Using builtin cd bypasses any overloaded functionality that may have been added through a shell function or alias by the user.



Example:



The cd built-in command may be overloaded by a function that updates the prompt:



cd() {
builtin cd "$@" && PS1=$(__update_prompt)
}


where __update_prompt is some other user-supplied function that outputs a string.



The builtin cd in the function would not call the function recursively. Using builtin cd in a shell where this function is active, would additionally not call the function.





1There are Unices with an external cd command (macOS, and, I believe, Solaris). The purpose of that command, which can't change the working directory for a shell, is possibly to satisfy the POSIX standard, which lists cd as one of the external utilities that should be available (cd is not one of the "special builtin utilities"). It may also serve as a test to see whether changing work directory to a given directory would be possible.






share|improve this answer


























  • FWIW, MacOS would also fall in the category of OSs with an external cd command.

    – yoann
    7 hours ago











  • @yoann Indeed it does.

    – Kusalananda
    7 hours ago



















-3














What is the concept of a self similar intermediary






share|improve this answer








New contributor




A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

    – Romeo Ninov
    32 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495422%2fwhat-is-the-difference-between-the-commands-builtin-cd-and-cd%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









20














The cd command is a built-in, so normally builtin cd will do the same thing as cd. But there is a difference if cd is redefined as a function or alias, in which case cd will call the function/alias but builtin cd will still change the directory (in other words, will keep the built-in accessible even if clobbered by a function.)



For example:





user:~$ cd () { echo "I won't let you change directories"; }
user:~$ cd mysubdir
I won't let you change directories
user:~$ builtin cd mysubdir
user:~/mysubdir$ unset -f cd # undefine function


Or with an alias:



user:~$ alias cd='echo Trying to cd to'
user:~$ cd mysubdir
Trying to cd to mysubdir
user:~$ builtin cd mysubdir
user:~/mysubdir$ unalias cd # undefine alias


Using builtin is also a good way to define a cd function that does something and changes directory (since calling cd from it would just keep calling the function again in an endless recursion.)



For example:



user:~ $ cd () { echo "Changing directory to ${1-home}"; builtin cd ${1+"$1"}; }
user:~ $ cd mysubdir
Changing directory to mysubdir
user:~/mysubdir $ cd
Changing directory to home
user:~ $ unset -f cd # undefine function





share|improve this answer





















  • 1





    +1 The examples are particularly illustrative here.

    – Tashus
    2 hours ago






  • 1





    In the case of an alias, is there any difference between builtin cd mysubdir and cd mysubdir?

    – gerrit
    26 mins ago
















20














The cd command is a built-in, so normally builtin cd will do the same thing as cd. But there is a difference if cd is redefined as a function or alias, in which case cd will call the function/alias but builtin cd will still change the directory (in other words, will keep the built-in accessible even if clobbered by a function.)



For example:





user:~$ cd () { echo "I won't let you change directories"; }
user:~$ cd mysubdir
I won't let you change directories
user:~$ builtin cd mysubdir
user:~/mysubdir$ unset -f cd # undefine function


Or with an alias:



user:~$ alias cd='echo Trying to cd to'
user:~$ cd mysubdir
Trying to cd to mysubdir
user:~$ builtin cd mysubdir
user:~/mysubdir$ unalias cd # undefine alias


Using builtin is also a good way to define a cd function that does something and changes directory (since calling cd from it would just keep calling the function again in an endless recursion.)



For example:



user:~ $ cd () { echo "Changing directory to ${1-home}"; builtin cd ${1+"$1"}; }
user:~ $ cd mysubdir
Changing directory to mysubdir
user:~/mysubdir $ cd
Changing directory to home
user:~ $ unset -f cd # undefine function





share|improve this answer





















  • 1





    +1 The examples are particularly illustrative here.

    – Tashus
    2 hours ago






  • 1





    In the case of an alias, is there any difference between builtin cd mysubdir and cd mysubdir?

    – gerrit
    26 mins ago














20












20








20







The cd command is a built-in, so normally builtin cd will do the same thing as cd. But there is a difference if cd is redefined as a function or alias, in which case cd will call the function/alias but builtin cd will still change the directory (in other words, will keep the built-in accessible even if clobbered by a function.)



For example:





user:~$ cd () { echo "I won't let you change directories"; }
user:~$ cd mysubdir
I won't let you change directories
user:~$ builtin cd mysubdir
user:~/mysubdir$ unset -f cd # undefine function


Or with an alias:



user:~$ alias cd='echo Trying to cd to'
user:~$ cd mysubdir
Trying to cd to mysubdir
user:~$ builtin cd mysubdir
user:~/mysubdir$ unalias cd # undefine alias


Using builtin is also a good way to define a cd function that does something and changes directory (since calling cd from it would just keep calling the function again in an endless recursion.)



For example:



user:~ $ cd () { echo "Changing directory to ${1-home}"; builtin cd ${1+"$1"}; }
user:~ $ cd mysubdir
Changing directory to mysubdir
user:~/mysubdir $ cd
Changing directory to home
user:~ $ unset -f cd # undefine function





share|improve this answer















The cd command is a built-in, so normally builtin cd will do the same thing as cd. But there is a difference if cd is redefined as a function or alias, in which case cd will call the function/alias but builtin cd will still change the directory (in other words, will keep the built-in accessible even if clobbered by a function.)



For example:





user:~$ cd () { echo "I won't let you change directories"; }
user:~$ cd mysubdir
I won't let you change directories
user:~$ builtin cd mysubdir
user:~/mysubdir$ unset -f cd # undefine function


Or with an alias:



user:~$ alias cd='echo Trying to cd to'
user:~$ cd mysubdir
Trying to cd to mysubdir
user:~$ builtin cd mysubdir
user:~/mysubdir$ unalias cd # undefine alias


Using builtin is also a good way to define a cd function that does something and changes directory (since calling cd from it would just keep calling the function again in an endless recursion.)



For example:



user:~ $ cd () { echo "Changing directory to ${1-home}"; builtin cd ${1+"$1"}; }
user:~ $ cd mysubdir
Changing directory to mysubdir
user:~/mysubdir $ cd
Changing directory to home
user:~ $ unset -f cd # undefine function






share|improve this answer














share|improve this answer



share|improve this answer








edited 5 hours ago









terdon

129k32253428




129k32253428










answered 8 hours ago









filbrandenfilbranden

7,6002838




7,6002838








  • 1





    +1 The examples are particularly illustrative here.

    – Tashus
    2 hours ago






  • 1





    In the case of an alias, is there any difference between builtin cd mysubdir and cd mysubdir?

    – gerrit
    26 mins ago














  • 1





    +1 The examples are particularly illustrative here.

    – Tashus
    2 hours ago






  • 1





    In the case of an alias, is there any difference between builtin cd mysubdir and cd mysubdir?

    – gerrit
    26 mins ago








1




1





+1 The examples are particularly illustrative here.

– Tashus
2 hours ago





+1 The examples are particularly illustrative here.

– Tashus
2 hours ago




1




1





In the case of an alias, is there any difference between builtin cd mysubdir and cd mysubdir?

– gerrit
26 mins ago





In the case of an alias, is there any difference between builtin cd mysubdir and cd mysubdir?

– gerrit
26 mins ago













10














In most instances, there is no difference (but see below). The cd command is a built-in command in all shells. It needs to be built-in1 as an external command can not change the environment of the invoking shell, and changing the working directory constitutes a change in its environment.



The bash command builtin forces the shell to use the built-in version of a command, even though there may be a shell function, alias, or external command available with the same name.



In the case where there is e.g. a shell function with the name cd, then builtin cd would not call that. Using builtin cd bypasses any overloaded functionality that may have been added through a shell function or alias by the user.



Example:



The cd built-in command may be overloaded by a function that updates the prompt:



cd() {
builtin cd "$@" && PS1=$(__update_prompt)
}


where __update_prompt is some other user-supplied function that outputs a string.



The builtin cd in the function would not call the function recursively. Using builtin cd in a shell where this function is active, would additionally not call the function.





1There are Unices with an external cd command (macOS, and, I believe, Solaris). The purpose of that command, which can't change the working directory for a shell, is possibly to satisfy the POSIX standard, which lists cd as one of the external utilities that should be available (cd is not one of the "special builtin utilities"). It may also serve as a test to see whether changing work directory to a given directory would be possible.






share|improve this answer


























  • FWIW, MacOS would also fall in the category of OSs with an external cd command.

    – yoann
    7 hours ago











  • @yoann Indeed it does.

    – Kusalananda
    7 hours ago
















10














In most instances, there is no difference (but see below). The cd command is a built-in command in all shells. It needs to be built-in1 as an external command can not change the environment of the invoking shell, and changing the working directory constitutes a change in its environment.



The bash command builtin forces the shell to use the built-in version of a command, even though there may be a shell function, alias, or external command available with the same name.



In the case where there is e.g. a shell function with the name cd, then builtin cd would not call that. Using builtin cd bypasses any overloaded functionality that may have been added through a shell function or alias by the user.



Example:



The cd built-in command may be overloaded by a function that updates the prompt:



cd() {
builtin cd "$@" && PS1=$(__update_prompt)
}


where __update_prompt is some other user-supplied function that outputs a string.



The builtin cd in the function would not call the function recursively. Using builtin cd in a shell where this function is active, would additionally not call the function.





1There are Unices with an external cd command (macOS, and, I believe, Solaris). The purpose of that command, which can't change the working directory for a shell, is possibly to satisfy the POSIX standard, which lists cd as one of the external utilities that should be available (cd is not one of the "special builtin utilities"). It may also serve as a test to see whether changing work directory to a given directory would be possible.






share|improve this answer


























  • FWIW, MacOS would also fall in the category of OSs with an external cd command.

    – yoann
    7 hours ago











  • @yoann Indeed it does.

    – Kusalananda
    7 hours ago














10












10








10







In most instances, there is no difference (but see below). The cd command is a built-in command in all shells. It needs to be built-in1 as an external command can not change the environment of the invoking shell, and changing the working directory constitutes a change in its environment.



The bash command builtin forces the shell to use the built-in version of a command, even though there may be a shell function, alias, or external command available with the same name.



In the case where there is e.g. a shell function with the name cd, then builtin cd would not call that. Using builtin cd bypasses any overloaded functionality that may have been added through a shell function or alias by the user.



Example:



The cd built-in command may be overloaded by a function that updates the prompt:



cd() {
builtin cd "$@" && PS1=$(__update_prompt)
}


where __update_prompt is some other user-supplied function that outputs a string.



The builtin cd in the function would not call the function recursively. Using builtin cd in a shell where this function is active, would additionally not call the function.





1There are Unices with an external cd command (macOS, and, I believe, Solaris). The purpose of that command, which can't change the working directory for a shell, is possibly to satisfy the POSIX standard, which lists cd as one of the external utilities that should be available (cd is not one of the "special builtin utilities"). It may also serve as a test to see whether changing work directory to a given directory would be possible.






share|improve this answer















In most instances, there is no difference (but see below). The cd command is a built-in command in all shells. It needs to be built-in1 as an external command can not change the environment of the invoking shell, and changing the working directory constitutes a change in its environment.



The bash command builtin forces the shell to use the built-in version of a command, even though there may be a shell function, alias, or external command available with the same name.



In the case where there is e.g. a shell function with the name cd, then builtin cd would not call that. Using builtin cd bypasses any overloaded functionality that may have been added through a shell function or alias by the user.



Example:



The cd built-in command may be overloaded by a function that updates the prompt:



cd() {
builtin cd "$@" && PS1=$(__update_prompt)
}


where __update_prompt is some other user-supplied function that outputs a string.



The builtin cd in the function would not call the function recursively. Using builtin cd in a shell where this function is active, would additionally not call the function.





1There are Unices with an external cd command (macOS, and, I believe, Solaris). The purpose of that command, which can't change the working directory for a shell, is possibly to satisfy the POSIX standard, which lists cd as one of the external utilities that should be available (cd is not one of the "special builtin utilities"). It may also serve as a test to see whether changing work directory to a given directory would be possible.







share|improve this answer














share|improve this answer



share|improve this answer








edited 5 hours ago

























answered 8 hours ago









KusalanandaKusalananda

125k16236389




125k16236389













  • FWIW, MacOS would also fall in the category of OSs with an external cd command.

    – yoann
    7 hours ago











  • @yoann Indeed it does.

    – Kusalananda
    7 hours ago



















  • FWIW, MacOS would also fall in the category of OSs with an external cd command.

    – yoann
    7 hours ago











  • @yoann Indeed it does.

    – Kusalananda
    7 hours ago

















FWIW, MacOS would also fall in the category of OSs with an external cd command.

– yoann
7 hours ago





FWIW, MacOS would also fall in the category of OSs with an external cd command.

– yoann
7 hours ago













@yoann Indeed it does.

– Kusalananda
7 hours ago





@yoann Indeed it does.

– Kusalananda
7 hours ago











-3














What is the concept of a self similar intermediary






share|improve this answer








New contributor




A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

    – Romeo Ninov
    32 mins ago
















-3














What is the concept of a self similar intermediary






share|improve this answer








New contributor




A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

    – Romeo Ninov
    32 mins ago














-3












-3








-3







What is the concept of a self similar intermediary






share|improve this answer








New contributor




A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










What is the concept of a self similar intermediary







share|improve this answer








New contributor




A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 1 hour ago









A.HossemA.Hossem

1




1




New contributor




A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






A.Hossem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

    – Romeo Ninov
    32 mins ago



















  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

    – Romeo Ninov
    32 mins ago

















This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

– Romeo Ninov
32 mins ago





This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

– Romeo Ninov
32 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495422%2fwhat-is-the-difference-between-the-commands-builtin-cd-and-cd%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL Server 17 - Attemping to backup to remote NAS but Access is denied

Always On Availability groups resolving state after failover - Remote harden of transaction...

Restoring from pg_dump with foreign key constraints