Why doesn't the cd command work in my shell program?

When I created my own shell, I cannot execute the cd command that I can do in the Linux shell. Why is this?

+5
source share
3 answers

Probably because the command cdshould be built into the shell, and not something external and executed. If an external command has changed the directory, it does not affect the parent shell. And there is no team /bin/cdor /usr/bin/cd.


I do not understand the line "If an external command changed the directory, it does not affect the parent shell."

, , fork(), exec() , . , "ls /", /bin/ls ls /. , chdir(), , . , cd, fork() exec().

, DOS .BAT cd cmd.exe. Unix - .

+5

, , , . bash ( , ), source script CURRENT. ( ) , :

~:$ cat $HOME/bin/goproj
#!/bin/bash
...
export SOMEVAR=someval
cd /home/foo/src/projects/"$1"
...

~:$ alias gp
alias gp="source $HOME/bin/goproj"

~:$ gp foo
~/src/projects/foo:$ echo $SOMEVAR
someval
~/src/projects/foo:$

, script . : "goproj" , , ; source.

+2

, :

  • cd .

:

- whereis ls

- whereis cd

(. )

  • cd , , , cd.

Look this way when ls is running, then it should know pwd. So this is your custom shell that will handle the directory. So its a shell that should support cd.

I think I get it.

0
source

All Articles