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.
One way to get around this is to use an alias instead:
alias proj="cd /home/tree/projects/java"
LINUX CD command does not work in shell script
Use an absolute path or, even better, an environment variable.
cd ~/Desktop
Related Topics
Remove Occurrences of String in Text File
When Should Xargs Be Preferred Over While-Read Loops
Nasm Linux Assembly Printing Integers
What Does "< /Dev/Null >& /Dev/Null" at the End of a Command Do
What Does "$" Give Us Exactly in a Shell Script
Command to Insert Lines Before First Match
How to Catch the L3-Cache Hits and Misses by Perf Tool in Linux
Installing G++ on Windows Subsystem for Linux
Cannot Install Extensions in Visual Studio Code
A Way to Determine a Process'S "Real" Memory Usage, I.E. Private Dirty Rss
Is Clock_Gettime() Adequate for Submicrosecond Timing
Use Sudo Inside Dockerfile (Alpine)
Packet Sniffing Using Raw Sockets in Linux in C
Installing Qt on Linux, Cannot Find -Lgl
Open O_Creat | O_Excl on Nfs in Linux