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
cd command doesn't work as desired inside a shell script
I think there is one escape to much, change:
cd \""$d"\"
to:
cd "$d"
How come cd doesn't work in my shell script?
In order to make a script runnable you need to modify the permissions on the script so that it is executable.
The command to change permissions on Linux or UNIX is chmod.
chmod +x ./name_of_your_script
Will grant the current user permission to execute the script.
The #! at the start of your script indicates to the interactive shell that the script is to be interpreted by /bin/bash so you do not really need add an extention to the file name. However, it is a common convention to append .sh to shell scripts.
Student Shell - cd not working
It's true that you need to avoid forking, but you also need to avoid executing. There is no /bin/cd
that programs call to change directories.
Instead of executing something, call the chdir
system call with your path. If the user types cd /tmp
, call chdir("/tmp")
The current directory is a property of each process (as the open files or the umask value) You cannot call a program to change your current dir, as a program is a different process and will change effectively its directory (not yours). That's the reason of not existing a cd external command and to have to make a system call to change it.
Why 'which -a cd' command doesn't give any output in linux?
Think about how shells and changing directories work: For each command you enter it will start a new process for the command. Changing directory only applies to the currently running process. If the cd
command was executed as an external command, then it would run it its own process, change its process directory, and then the process would exit and the parent process (the shell) would not know anything at all what the child process did.
Therefore the cd
command can only be internal to the shell. It has to be parsed and executed completely by the shell and its own process.
Related Topics
How to Script a "Yes" Response for Installing Programs
How to Avoid Transparent_Hugepage/Defrag Warning from Mongodb
Don't Fail Jenkins Build If Execute Shell Fails
Negate If Condition in Bash Script
Creating Temporary Files in Bash
How Pthread_Mutex_Lock Is Implemented
How Find Out Which Process Is Using a File in Linux
How to Kill All Linux Processes That Are Older Than a Certain Age
How to Run a Windows Executable from Wsl (Ubuntu) Bash
The Concept of 'Hold Space' and 'Pattern Space' in Sed
Differencebetween Double-Ampersand (&&) and Semicolon (;) in Linux Bash
How Would One Disable Nagle's Algorithm in Linux