1

When bash is executing a script, ls -la /proc/<pid of bash>/fd shows "255 -> /path/to/script". I think that bash reads the script via file discriptor 255, and executes the commands line by line.

But when bash is sourcing a script, I can't find such an entry in ls -la /proc/<pid of bash>/fd. I can't find in cat /proc/<pid of bash>/maps, either.

I have a question: When bash sources a script, how does bash read the script? Is there any special way to read the script file?

1
  • When bash sources a script, how does bash read the script I would think the commands are read in the same way as when you type commands in your shell. when you run a script, another shell process is created but when you source a script you are using the same shell process and for that reason you don't see the file descriptor. I think that but if I'm wrong it should be useful someone provide the correct answer. It's interesting. Commented Oct 20, 2022 at 8:36

1 Answer 1

1

I hit upon an idea to analyze the situation by strace.

When just executing, the output of strace -f -p <pid> is like this:

openat(AT_FDCWD, "./test", O_RDONLY) = 3

...

[pid 3408] dup2(3, 255) = 255

[pid 3408] close(3) = 0

"test" is the shell script to be executed. "3408" is the pid of the child shell. "test" is opened at file discriptor 3, and dupulicated to file discriptor 255. So ls -la /proc/<pid of bash>/fd shows "255 -> /some/directory/test".

But when sourcing the script, "test" is opened, read into the buffer, and immediately closed. So ls -la /proc/<pid of bash>/fd show no such entry as above.

I don't know why bash is implemented like this.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.