Can anyone explain the source of the cd shell command?

$ cat $(which cd)
#!/bin/sh
# $FreeBSD: src/usr.bin/alias/generic.sh,v 1.2 2005/10/24 22:32:19   cperciva Exp $
# This file is in the public domain.
builtin `echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"}

My interest is in understanding who writes the state change after calling cd. Is this a file?

0
source share
1 answer

It is not built-in cd, and in fact it does not work, except to indicate whether the directory can be changed by rejecting a non-zero exit status if it cannot.

Try:

"$(which cd)" /

... will do nothing.


The actual inline inline cdis part of your C shell and calls syscall chdir(). This syscall updates your process status - tracked by the kernel - to work with another working directory.


... , script? :

builtin `echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"}

... -, echo ( , - ), tr , , cd, cd.

... second, ${1+"$@"} ("$@") , , (${foo+bar} bar, foo ).

, shell-builtin cd (, , syscall chdir()), . , , , , , , , shebang #!/bin/sh, , .


, cd? , Busybox ash.

  • - ...
  • - .
  • - syscall, .
+4
source

Source: https://habr.com/ru/post/1650433/


All Articles