Timeline for Can I make cd be local to a function?
Current License: CC BY-SA 4.0
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 4, 2020 at 6:59 | comment | added | Stéphane Chazelas | I've raised github.com/ksh93/ksh/issues/141 | |
| Oct 4, 2020 at 6:28 | comment | added | Stéphane Chazelas | Thanks, I stand corrected. I hadn't realised that. I had assumed using fd = open("."); fchdir(fd) was a reliable way to temporarily change the working directory. That's what ksh93 does to implement its non-forking subshells, and I see it fails at restoring the current working directory in echo "$(chmod 0 .; cd / && pwd)". | |
| Oct 4, 2020 at 6:10 | comment | added | user313992 | WRT your first example: it will not work in C / with fchdir(2) either. You cannot change the current dir to a dir to which you don't have search/execute permissions, no matter if it's via its path (with chdir(2)), or via an open fd (with fchdir(2)). That's what I was trying to say with "fchdir(2) wouldn't have helped either". But yes, that last part is a bit untidy / rambling. | |
| Oct 4, 2020 at 5:35 | comment | added | Stéphane Chazelas | Note that the only thing bash-specific in there is the RETURN trap. ksh93 and zsh use EXIT instead for that (not in sh mode in zsh, and you need to use the ksh-style function definition syntax in ksh93). So it should be easily adapted to those shells. In those you wouldn't need that double-cd workaround in your last paragraph, and you could give the O_CLOEXEC flag to that fd to avoid leaking it to other commands. | |
| Oct 4, 2020 at 5:17 | comment | added | Stéphane Chazelas | bash -c 'mkdir 1; cd 1; exec 3<.; cd ..; chmod 0 1; cd -P /dev/fd/3' gives me a permission denied, but bash -c 'mkdir -p 1/2/3; cd 1/2/3; exec 3<.; cd ../../..; chmod 0 1; cd -P /dev/fd/3' does not (oddly enough). | |
| Oct 3, 2020 at 23:55 | history | edited | user313992 | CC BY-SA 4.0 | some rambling (faulty?) rationale |
| Oct 3, 2020 at 23:04 | comment | added | user313992 | @StéphaneChazelas thanks a lot for the fixes, but the last paragraph didn't seem right. see the changes (the "anonymous" edit was still mine) -- maybe I'm misunderstanding it? | |
| S Oct 3, 2020 at 22:57 | history | edited | user313992 | CC BY-SA 4.0 | That last paragraph is not correct at all; or it may need a lot more precising (older bash version?) |
| S Oct 3, 2020 at 22:57 | history | suggested | CommunityBot | CC BY-SA 4.0 | That last paragraph is not correct at all; or it may need a lot more precising (older bash version?) |
| Oct 3, 2020 at 21:47 | review | Suggested edits | |||
| S Oct 3, 2020 at 22:57 | |||||
| Oct 3, 2020 at 10:50 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 | suggest better order of execution. $PWD is preferable over `$(pwd)` (which doesn't work for directory names ending in NL). Use dynamic fd for reentrance. Check some exit status. Note about permssions issues with chdir(/dev/fd/x). |
| Oct 3, 2020 at 10:32 | history | edited | user313992 | CC BY-SA 4.0 | added 1 character in body |
| Oct 3, 2020 at 10:27 | history | edited | user313992 | CC BY-SA 4.0 | added 430 characters in body |
| Oct 3, 2020 at 10:02 | history | answered | user313992 | CC BY-SA 4.0 |