Timeline for Script to change current directory (cd, pwd)
Current License: CC BY-SA 4.0
24 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 3, 2023 at 23:15 | comment | added | Sham Fiorin | $SHELL solved for zsh | |
| Sep 1, 2021 at 8:08 | history | edited | Serge Stroobandt | CC BY-SA 4.0 | added 24 characters in body |
| Aug 31, 2021 at 5:24 | history | rollback | Kusalananda♦ | Rollback to Revision 7 | |
| S Aug 30, 2021 at 23:34 | history | suggested | Alexar | CC BY-SA 4.0 | Applies the update to the heading |
| Aug 30, 2021 at 23:17 | review | Suggested edits | |||
| S Aug 30, 2021 at 23:34 | |||||
| Aug 21, 2021 at 6:52 | comment | added | Kusalananda♦ | Consider testing this approach yourself. Run the script a few times and check with something like pstree what type of processes this creates. You will notice that each time you run the script a new shell process is created, without replacing the invoking interactive shell. The exec would replace the non-interactive shell running the script, but not the interactive shell invoking the script. As have been pointed out, this also resets several aspects of the shell, and unless special care is taken, shell's command line history is not carried forward into the new shell session either. | |
| Aug 21, 2021 at 0:13 | comment | added | G-Man Says 'Reinstate Monica' | (Cont’d) … In the case of a script, this is a second shell process — a non-interactive, script-running shell process — in addition to the interactive shell process. When the script reaches the end or executes an exit command, the script-running shell process terminates. But when the script does exec bash, the script-running shell process is replaced with a new, interactive shell process, in addition to the one that launched the script. So, yes, bash processes pile up. | |
| Aug 21, 2021 at 0:13 | comment | added | G-Man Says 'Reinstate Monica' | @Xerus: Did you read the answer carefully and try doing what it suggests? Certainly there is some truth to what you say: if you type exec bash (or anything similar) into an interactive shell, it will replace the current shell process with a new copy of bash. But surely you know that, whenever you run a utility program (like cp or ls) or a script, your interactive shell forks a new process to run that program. (Of course this does not apply to built-in commands like cd and pwd.) … (Cont’d) | |
| Aug 11, 2021 at 10:17 | comment | added | xeruf | @G-ManSays'ReinstateMonica' if you use exec, the current process is replaced, so nothing piles up. And I personally never change shell options interactively in normal use. | |
| Apr 27, 2021 at 12:46 | comment | added | Dávid Horváth | Because you started a subshell, all unexported variables and functions are lost. At the end you are forced to exit from all these subshells. | |
| Jan 29, 2021 at 13:01 | history | edited | Serge Stroobandt | CC BY-SA 4.0 | deleted 2 characters in body |
| Jan 27, 2021 at 13:41 | comment | added | Marinos An | Great solution. I can now write a script: cdf that given a file path it takes me to its directory: cd "$(dirname "$1")"; $SHELL | |
| Oct 25, 2020 at 10:49 | comment | added | Serge Stroobandt | @Acumenus You are absolutely right. The exec was required with older versions of bash and possibly other shells. I updated the answer accordingly. | |
| Oct 25, 2020 at 10:47 | history | edited | Serge Stroobandt | CC BY-SA 4.0 | added 340 characters in body |
| Oct 18, 2019 at 13:46 | comment | added | artyom.razinov | Very nice solution! I've rewritten my alias in bash_profile so now it is a script stored in a separate file. I use the script to go to a newly created temporary folder. And now it is even easier to have a temporary bash session. SRP in action! Thanks! | |
| Apr 16, 2019 at 16:54 | history | edited | Serge Stroobandt | CC BY-SA 4.0 | added 121 characters in body |
| Apr 10, 2019 at 16:39 | comment | added | G-Man Says 'Reinstate Monica' | Since nobody has detailed the problems with this (I’m looking at you, @Dennis): (1) Each time you run this, it creates a new, persistent bash process. Do it ten or twenty times in a session, and you’ll have 11 to 21 bash processes piled up. This may affect performance, and, if you try to terminate the session cleanly by typing exit (or Ctrl+D), you’ll have to do that 11 to 21 times. (2) Another drawback of using an executable script is that, if you set any shell options (e.g., dotglob or globstar) in your interactive shell session, you will lose them, because you’re starting a new shell. | |
| Mar 15, 2019 at 23:14 | history | edited | Serge Stroobandt | CC BY-SA 4.0 | added 4 characters in body |
| Mar 15, 2019 at 22:04 | comment | added | Dennis Williamson | This is the wrong way to go about this! | |
| May 5, 2018 at 0:41 | comment | added | vdegenne | this trick doesn't work in debian 9 stretch. | |
| Feb 5, 2018 at 20:37 | history | edited | Serge Stroobandt | CC BY-SA 3.0 | added 2 characters in body |
| Aug 16, 2016 at 0:18 | comment | added | neuronet | Better to just source the script, as in accepted answer: using exec is typically considered the last resort of a scoundrel.. :) | |
| Apr 22, 2016 at 8:55 | history | edited | Serge Stroobandt | CC BY-SA 3.0 | added 2 characters in body |
| Apr 21, 2016 at 11:36 | history | answered | Serge Stroobandt | CC BY-SA 3.0 |