Contents
Why does cd not work in bash?
Trying to use cd inside the shell script does not work because the shell script runs in the subshell and once the script is over it returns to the parent shell, which is why the current directory does not change. To achieve changing of the directory use sourcing. You can either use .
How do I change directory in git bash?
How to change folders in Git Bash
- You can check the current folder with pwd .
- If the path contains spaces, you will need to use quotation marks. ( cd “C:/Program Files” )
- On Windows, you change the default starting directory for Git Bash.
- The cd command can be memorized as “change directory”.
How to change the current directory from a bash script?
Maybe there’s a better way: because while it appears to work, after you run it and your script finishes, yes you’ll be in the correct directory, but you’ll be in it in a subshell, which you can confirm by pressing Ctrl+D afterwards, and you’ll see it exits the subshell, putting you back in your original directory.
Is there a way to change the parent shell Directory?
The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes. To change the current shell’s directory permanently you should use the source command, also aliased simply as., which runs a script in the current shell environment instead of a sub shell.
Why can’t I change directories using ” CD ” in a script?
Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The cd succeeds, but as soon as the subshell exits, you’re back in the interactive shell and nothing ever changed there. You’re doing nothing wrong!
Why is my shell running in a new directory?
You are making a thinking error. While the current shell stays in the same directory, the script has moved to the new directory. You could see that by creating another script in the new directory, and running it from your script, after it has changed directory: The second script would run from the new directory.