Why doesn't “cd” work in a shell script?
up vote
37
down vote
favorite
I just want to write a script which changes my directory.
I put the below commands in the file /home/alex/pathABC
#!/bin/sh
cd /home/alex/Documents/A/B/C
echo HelloWorld
I did chmod +x pathABC
.
In the terminal, while in /home/alex
, I run ./pathABC
, but the output is just HelloWorld
and the current directory is not changed.
So what is wrong?
command-line scripts gnome-terminal
add a comment |
up vote
37
down vote
favorite
I just want to write a script which changes my directory.
I put the below commands in the file /home/alex/pathABC
#!/bin/sh
cd /home/alex/Documents/A/B/C
echo HelloWorld
I did chmod +x pathABC
.
In the terminal, while in /home/alex
, I run ./pathABC
, but the output is just HelloWorld
and the current directory is not changed.
So what is wrong?
command-line scripts gnome-terminal
6
Look at this Why doesn't “cd” work in a bash shell script?
– TuKsn
Jun 11 '14 at 8:45
add a comment |
up vote
37
down vote
favorite
up vote
37
down vote
favorite
I just want to write a script which changes my directory.
I put the below commands in the file /home/alex/pathABC
#!/bin/sh
cd /home/alex/Documents/A/B/C
echo HelloWorld
I did chmod +x pathABC
.
In the terminal, while in /home/alex
, I run ./pathABC
, but the output is just HelloWorld
and the current directory is not changed.
So what is wrong?
command-line scripts gnome-terminal
I just want to write a script which changes my directory.
I put the below commands in the file /home/alex/pathABC
#!/bin/sh
cd /home/alex/Documents/A/B/C
echo HelloWorld
I did chmod +x pathABC
.
In the terminal, while in /home/alex
, I run ./pathABC
, but the output is just HelloWorld
and the current directory is not changed.
So what is wrong?
command-line scripts gnome-terminal
command-line scripts gnome-terminal
edited Jul 24 at 21:05
wjandrea
7,90342258
7,90342258
asked Jun 11 '14 at 8:35
Mohammad Reza Rezwani
3,5742258109
3,5742258109
6
Look at this Why doesn't “cd” work in a bash shell script?
– TuKsn
Jun 11 '14 at 8:45
add a comment |
6
Look at this Why doesn't “cd” work in a bash shell script?
– TuKsn
Jun 11 '14 at 8:45
6
6
Look at this Why doesn't “cd” work in a bash shell script?
– TuKsn
Jun 11 '14 at 8:45
Look at this Why doesn't “cd” work in a bash shell script?
– TuKsn
Jun 11 '14 at 8:45
add a comment |
4 Answers
4
active
oldest
votes
up vote
69
down vote
accepted
As others have explained, the directory is changed in the child process of your script, not in the terminal process from which the script is called. After the child process dies, you are back in the terminal which is left where it was.
Several alternatives:
1. Symbolic link
Put a symlink in your home to the long path you want to easily access
$ ln -s /home/alex/Documents/A/B/C ~/pathABC
then access the directory with:
$ cd ~/pathABC
2. Alias
Put an alias in your ~/.bashrc:
alias pathABC="cd /home/alex/Documents/A/B/C"
(from here)
3. Function
Create a function that changes the directory, the function runs in the process of your terminal and can then change its directory.
(from here)
4. Avoid running as child
Source your script instead of running it. Sourcing (done by .
or source
) causes the script to be executed in the same shell instead of running in its own subshell.
$ . ./pathABC
(from here and here)
5. cd-able vars
Set the cdable_vars
option in your ~/.bashrc
and create an environment variable to the directory:
shopt -s cdable_vars
export pathABC="/home/alex/Documents/A/B/C"
Then you can use cd pathABC
(from here)
13
Now I understand the usage ofsource
! I always wondered why I dosource .bashrc
and notbash .bashrc
– hytromo
Jun 11 '14 at 12:05
Option 3 worked great. I just defined a function go_to_wherever() { cd my/directory } at the beginning of my script. Called it before running the operations in that directory.
– i2097i
Jan 28 '17 at 20:13
> 5. cd-able vars -- isn't itcd $pathABC
?
– loxaxs
Jul 2 '17 at 9:56
add a comment |
up vote
7
down vote
When you run script in a terminal, a child process runs. In this child program ie your script will change to whatever directory specified. But in the parent process ie where you run the script is still in the old path.
OR simply we can say:
The scope of cd command is only for child process not parent
2
Adding to this, @alex to achieve the effect you are looking for, execute the script within the parent process by sourcing it: either. pathABC
orsource pathABC
.
– zwets
Jun 11 '14 at 9:26
add a comment |
up vote
4
down vote
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:
#!/bin/sh
cd /home/alex/Documents/A/B/C && ./another_script.sh # (if it is executable)
The second script would run from the new directory.
HelloWorld
is just the output of the script.
3
HelloWorld is not "returned" to the parent shell, it's output to the standard output
– Mog
Jun 11 '14 at 12:01
Might be clearer if you just runpwd
in the new directory, instead of adding a whole new script to the situation.
– wjandrea
Jul 24 at 21:17
add a comment |
up vote
0
down vote
Because hello world is just a trace statement, let's try this:
Create bash script file cd.sh
containing:
#!/bin/bash
echo "/home/mike/Documents/A/B/C"
- The
.sh
extension is an older convention of giving bash script filenames an extension. It's purely cosmetic and usually unnecessary. However in this case it's important to differentiate from the corecd
command.
Mark the bash script file executable using:
chmod a+x cd.sh
Now run the file:
$ cd $(./cd.sh)
bash: cd: /home/alex/Documents/A/B/C: No such file or directory
cd
we all know.
$(...)
executes command inside parenthesis and returns output.- If
cd.sh
was in your path you don't need to specify where it is. We prefix with./
to specify the command is in the current directory. - The
echo
output from thecd.sh
script is sent back to the parent via the$(...)
. The parent (our shell prompt) uses this output and passes it to the Linuxcd
command.
As others have mentioned a child process can't change the parent's directory. This is one way the child can tell the parent where to go after the process ends.
@wjandrea That's what you get for coding on a phone! Just got home, I'll fix it up. Thanks. Um just checked and it works fine. Maybe it's your14.04
version I read about an hour ago or so?
– WinEunuuchs2Unix
Jul 24 at 22:51
Well, you totally changed the script. Previously it was just one command:cd /home/mike/Documents/A/B/C
, which didn't produce any output. Now it'secho "/home/mike/Documents/A/B/C"
, which does produce output.
– wjandrea
Jul 25 at 1:01
@wjandrea True, I also totally changed the way of calling the script too. Instead of a simple./cd.sh
it's nowcd $(./cd.sh)
but it accomplishes the goal of the child changing the parent's current directory. Yes it's unconventional but it's another way of doing it that I hope people find interesting.
– WinEunuuchs2Unix
Jul 25 at 1:26
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
69
down vote
accepted
As others have explained, the directory is changed in the child process of your script, not in the terminal process from which the script is called. After the child process dies, you are back in the terminal which is left where it was.
Several alternatives:
1. Symbolic link
Put a symlink in your home to the long path you want to easily access
$ ln -s /home/alex/Documents/A/B/C ~/pathABC
then access the directory with:
$ cd ~/pathABC
2. Alias
Put an alias in your ~/.bashrc:
alias pathABC="cd /home/alex/Documents/A/B/C"
(from here)
3. Function
Create a function that changes the directory, the function runs in the process of your terminal and can then change its directory.
(from here)
4. Avoid running as child
Source your script instead of running it. Sourcing (done by .
or source
) causes the script to be executed in the same shell instead of running in its own subshell.
$ . ./pathABC
(from here and here)
5. cd-able vars
Set the cdable_vars
option in your ~/.bashrc
and create an environment variable to the directory:
shopt -s cdable_vars
export pathABC="/home/alex/Documents/A/B/C"
Then you can use cd pathABC
(from here)
13
Now I understand the usage ofsource
! I always wondered why I dosource .bashrc
and notbash .bashrc
– hytromo
Jun 11 '14 at 12:05
Option 3 worked great. I just defined a function go_to_wherever() { cd my/directory } at the beginning of my script. Called it before running the operations in that directory.
– i2097i
Jan 28 '17 at 20:13
> 5. cd-able vars -- isn't itcd $pathABC
?
– loxaxs
Jul 2 '17 at 9:56
add a comment |
up vote
69
down vote
accepted
As others have explained, the directory is changed in the child process of your script, not in the terminal process from which the script is called. After the child process dies, you are back in the terminal which is left where it was.
Several alternatives:
1. Symbolic link
Put a symlink in your home to the long path you want to easily access
$ ln -s /home/alex/Documents/A/B/C ~/pathABC
then access the directory with:
$ cd ~/pathABC
2. Alias
Put an alias in your ~/.bashrc:
alias pathABC="cd /home/alex/Documents/A/B/C"
(from here)
3. Function
Create a function that changes the directory, the function runs in the process of your terminal and can then change its directory.
(from here)
4. Avoid running as child
Source your script instead of running it. Sourcing (done by .
or source
) causes the script to be executed in the same shell instead of running in its own subshell.
$ . ./pathABC
(from here and here)
5. cd-able vars
Set the cdable_vars
option in your ~/.bashrc
and create an environment variable to the directory:
shopt -s cdable_vars
export pathABC="/home/alex/Documents/A/B/C"
Then you can use cd pathABC
(from here)
13
Now I understand the usage ofsource
! I always wondered why I dosource .bashrc
and notbash .bashrc
– hytromo
Jun 11 '14 at 12:05
Option 3 worked great. I just defined a function go_to_wherever() { cd my/directory } at the beginning of my script. Called it before running the operations in that directory.
– i2097i
Jan 28 '17 at 20:13
> 5. cd-able vars -- isn't itcd $pathABC
?
– loxaxs
Jul 2 '17 at 9:56
add a comment |
up vote
69
down vote
accepted
up vote
69
down vote
accepted
As others have explained, the directory is changed in the child process of your script, not in the terminal process from which the script is called. After the child process dies, you are back in the terminal which is left where it was.
Several alternatives:
1. Symbolic link
Put a symlink in your home to the long path you want to easily access
$ ln -s /home/alex/Documents/A/B/C ~/pathABC
then access the directory with:
$ cd ~/pathABC
2. Alias
Put an alias in your ~/.bashrc:
alias pathABC="cd /home/alex/Documents/A/B/C"
(from here)
3. Function
Create a function that changes the directory, the function runs in the process of your terminal and can then change its directory.
(from here)
4. Avoid running as child
Source your script instead of running it. Sourcing (done by .
or source
) causes the script to be executed in the same shell instead of running in its own subshell.
$ . ./pathABC
(from here and here)
5. cd-able vars
Set the cdable_vars
option in your ~/.bashrc
and create an environment variable to the directory:
shopt -s cdable_vars
export pathABC="/home/alex/Documents/A/B/C"
Then you can use cd pathABC
(from here)
As others have explained, the directory is changed in the child process of your script, not in the terminal process from which the script is called. After the child process dies, you are back in the terminal which is left where it was.
Several alternatives:
1. Symbolic link
Put a symlink in your home to the long path you want to easily access
$ ln -s /home/alex/Documents/A/B/C ~/pathABC
then access the directory with:
$ cd ~/pathABC
2. Alias
Put an alias in your ~/.bashrc:
alias pathABC="cd /home/alex/Documents/A/B/C"
(from here)
3. Function
Create a function that changes the directory, the function runs in the process of your terminal and can then change its directory.
(from here)
4. Avoid running as child
Source your script instead of running it. Sourcing (done by .
or source
) causes the script to be executed in the same shell instead of running in its own subshell.
$ . ./pathABC
(from here and here)
5. cd-able vars
Set the cdable_vars
option in your ~/.bashrc
and create an environment variable to the directory:
shopt -s cdable_vars
export pathABC="/home/alex/Documents/A/B/C"
Then you can use cd pathABC
(from here)
edited May 23 '17 at 12:39
Community♦
1
1
answered Jun 11 '14 at 9:33
Gauthier
1,5521527
1,5521527
13
Now I understand the usage ofsource
! I always wondered why I dosource .bashrc
and notbash .bashrc
– hytromo
Jun 11 '14 at 12:05
Option 3 worked great. I just defined a function go_to_wherever() { cd my/directory } at the beginning of my script. Called it before running the operations in that directory.
– i2097i
Jan 28 '17 at 20:13
> 5. cd-able vars -- isn't itcd $pathABC
?
– loxaxs
Jul 2 '17 at 9:56
add a comment |
13
Now I understand the usage ofsource
! I always wondered why I dosource .bashrc
and notbash .bashrc
– hytromo
Jun 11 '14 at 12:05
Option 3 worked great. I just defined a function go_to_wherever() { cd my/directory } at the beginning of my script. Called it before running the operations in that directory.
– i2097i
Jan 28 '17 at 20:13
> 5. cd-able vars -- isn't itcd $pathABC
?
– loxaxs
Jul 2 '17 at 9:56
13
13
Now I understand the usage of
source
! I always wondered why I do source .bashrc
and not bash .bashrc
– hytromo
Jun 11 '14 at 12:05
Now I understand the usage of
source
! I always wondered why I do source .bashrc
and not bash .bashrc
– hytromo
Jun 11 '14 at 12:05
Option 3 worked great. I just defined a function go_to_wherever() { cd my/directory } at the beginning of my script. Called it before running the operations in that directory.
– i2097i
Jan 28 '17 at 20:13
Option 3 worked great. I just defined a function go_to_wherever() { cd my/directory } at the beginning of my script. Called it before running the operations in that directory.
– i2097i
Jan 28 '17 at 20:13
> 5. cd-able vars -- isn't it
cd $pathABC
?– loxaxs
Jul 2 '17 at 9:56
> 5. cd-able vars -- isn't it
cd $pathABC
?– loxaxs
Jul 2 '17 at 9:56
add a comment |
up vote
7
down vote
When you run script in a terminal, a child process runs. In this child program ie your script will change to whatever directory specified. But in the parent process ie where you run the script is still in the old path.
OR simply we can say:
The scope of cd command is only for child process not parent
2
Adding to this, @alex to achieve the effect you are looking for, execute the script within the parent process by sourcing it: either. pathABC
orsource pathABC
.
– zwets
Jun 11 '14 at 9:26
add a comment |
up vote
7
down vote
When you run script in a terminal, a child process runs. In this child program ie your script will change to whatever directory specified. But in the parent process ie where you run the script is still in the old path.
OR simply we can say:
The scope of cd command is only for child process not parent
2
Adding to this, @alex to achieve the effect you are looking for, execute the script within the parent process by sourcing it: either. pathABC
orsource pathABC
.
– zwets
Jun 11 '14 at 9:26
add a comment |
up vote
7
down vote
up vote
7
down vote
When you run script in a terminal, a child process runs. In this child program ie your script will change to whatever directory specified. But in the parent process ie where you run the script is still in the old path.
OR simply we can say:
The scope of cd command is only for child process not parent
When you run script in a terminal, a child process runs. In this child program ie your script will change to whatever directory specified. But in the parent process ie where you run the script is still in the old path.
OR simply we can say:
The scope of cd command is only for child process not parent
answered Jun 11 '14 at 8:56
Tingrammer
1793
1793
2
Adding to this, @alex to achieve the effect you are looking for, execute the script within the parent process by sourcing it: either. pathABC
orsource pathABC
.
– zwets
Jun 11 '14 at 9:26
add a comment |
2
Adding to this, @alex to achieve the effect you are looking for, execute the script within the parent process by sourcing it: either. pathABC
orsource pathABC
.
– zwets
Jun 11 '14 at 9:26
2
2
Adding to this, @alex to achieve the effect you are looking for, execute the script within the parent process by sourcing it: either
. pathABC
or source pathABC
.– zwets
Jun 11 '14 at 9:26
Adding to this, @alex to achieve the effect you are looking for, execute the script within the parent process by sourcing it: either
. pathABC
or source pathABC
.– zwets
Jun 11 '14 at 9:26
add a comment |
up vote
4
down vote
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:
#!/bin/sh
cd /home/alex/Documents/A/B/C && ./another_script.sh # (if it is executable)
The second script would run from the new directory.
HelloWorld
is just the output of the script.
3
HelloWorld is not "returned" to the parent shell, it's output to the standard output
– Mog
Jun 11 '14 at 12:01
Might be clearer if you just runpwd
in the new directory, instead of adding a whole new script to the situation.
– wjandrea
Jul 24 at 21:17
add a comment |
up vote
4
down vote
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:
#!/bin/sh
cd /home/alex/Documents/A/B/C && ./another_script.sh # (if it is executable)
The second script would run from the new directory.
HelloWorld
is just the output of the script.
3
HelloWorld is not "returned" to the parent shell, it's output to the standard output
– Mog
Jun 11 '14 at 12:01
Might be clearer if you just runpwd
in the new directory, instead of adding a whole new script to the situation.
– wjandrea
Jul 24 at 21:17
add a comment |
up vote
4
down vote
up vote
4
down vote
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:
#!/bin/sh
cd /home/alex/Documents/A/B/C && ./another_script.sh # (if it is executable)
The second script would run from the new directory.
HelloWorld
is just the output of the script.
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:
#!/bin/sh
cd /home/alex/Documents/A/B/C && ./another_script.sh # (if it is executable)
The second script would run from the new directory.
HelloWorld
is just the output of the script.
edited Jul 24 at 21:15
wjandrea
7,90342258
7,90342258
answered Jun 11 '14 at 8:48
Jacob Vlijm
62.9k9121215
62.9k9121215
3
HelloWorld is not "returned" to the parent shell, it's output to the standard output
– Mog
Jun 11 '14 at 12:01
Might be clearer if you just runpwd
in the new directory, instead of adding a whole new script to the situation.
– wjandrea
Jul 24 at 21:17
add a comment |
3
HelloWorld is not "returned" to the parent shell, it's output to the standard output
– Mog
Jun 11 '14 at 12:01
Might be clearer if you just runpwd
in the new directory, instead of adding a whole new script to the situation.
– wjandrea
Jul 24 at 21:17
3
3
HelloWorld is not "returned" to the parent shell, it's output to the standard output
– Mog
Jun 11 '14 at 12:01
HelloWorld is not "returned" to the parent shell, it's output to the standard output
– Mog
Jun 11 '14 at 12:01
Might be clearer if you just run
pwd
in the new directory, instead of adding a whole new script to the situation.– wjandrea
Jul 24 at 21:17
Might be clearer if you just run
pwd
in the new directory, instead of adding a whole new script to the situation.– wjandrea
Jul 24 at 21:17
add a comment |
up vote
0
down vote
Because hello world is just a trace statement, let's try this:
Create bash script file cd.sh
containing:
#!/bin/bash
echo "/home/mike/Documents/A/B/C"
- The
.sh
extension is an older convention of giving bash script filenames an extension. It's purely cosmetic and usually unnecessary. However in this case it's important to differentiate from the corecd
command.
Mark the bash script file executable using:
chmod a+x cd.sh
Now run the file:
$ cd $(./cd.sh)
bash: cd: /home/alex/Documents/A/B/C: No such file or directory
cd
we all know.
$(...)
executes command inside parenthesis and returns output.- If
cd.sh
was in your path you don't need to specify where it is. We prefix with./
to specify the command is in the current directory. - The
echo
output from thecd.sh
script is sent back to the parent via the$(...)
. The parent (our shell prompt) uses this output and passes it to the Linuxcd
command.
As others have mentioned a child process can't change the parent's directory. This is one way the child can tell the parent where to go after the process ends.
@wjandrea That's what you get for coding on a phone! Just got home, I'll fix it up. Thanks. Um just checked and it works fine. Maybe it's your14.04
version I read about an hour ago or so?
– WinEunuuchs2Unix
Jul 24 at 22:51
Well, you totally changed the script. Previously it was just one command:cd /home/mike/Documents/A/B/C
, which didn't produce any output. Now it'secho "/home/mike/Documents/A/B/C"
, which does produce output.
– wjandrea
Jul 25 at 1:01
@wjandrea True, I also totally changed the way of calling the script too. Instead of a simple./cd.sh
it's nowcd $(./cd.sh)
but it accomplishes the goal of the child changing the parent's current directory. Yes it's unconventional but it's another way of doing it that I hope people find interesting.
– WinEunuuchs2Unix
Jul 25 at 1:26
add a comment |
up vote
0
down vote
Because hello world is just a trace statement, let's try this:
Create bash script file cd.sh
containing:
#!/bin/bash
echo "/home/mike/Documents/A/B/C"
- The
.sh
extension is an older convention of giving bash script filenames an extension. It's purely cosmetic and usually unnecessary. However in this case it's important to differentiate from the corecd
command.
Mark the bash script file executable using:
chmod a+x cd.sh
Now run the file:
$ cd $(./cd.sh)
bash: cd: /home/alex/Documents/A/B/C: No such file or directory
cd
we all know.
$(...)
executes command inside parenthesis and returns output.- If
cd.sh
was in your path you don't need to specify where it is. We prefix with./
to specify the command is in the current directory. - The
echo
output from thecd.sh
script is sent back to the parent via the$(...)
. The parent (our shell prompt) uses this output and passes it to the Linuxcd
command.
As others have mentioned a child process can't change the parent's directory. This is one way the child can tell the parent where to go after the process ends.
@wjandrea That's what you get for coding on a phone! Just got home, I'll fix it up. Thanks. Um just checked and it works fine. Maybe it's your14.04
version I read about an hour ago or so?
– WinEunuuchs2Unix
Jul 24 at 22:51
Well, you totally changed the script. Previously it was just one command:cd /home/mike/Documents/A/B/C
, which didn't produce any output. Now it'secho "/home/mike/Documents/A/B/C"
, which does produce output.
– wjandrea
Jul 25 at 1:01
@wjandrea True, I also totally changed the way of calling the script too. Instead of a simple./cd.sh
it's nowcd $(./cd.sh)
but it accomplishes the goal of the child changing the parent's current directory. Yes it's unconventional but it's another way of doing it that I hope people find interesting.
– WinEunuuchs2Unix
Jul 25 at 1:26
add a comment |
up vote
0
down vote
up vote
0
down vote
Because hello world is just a trace statement, let's try this:
Create bash script file cd.sh
containing:
#!/bin/bash
echo "/home/mike/Documents/A/B/C"
- The
.sh
extension is an older convention of giving bash script filenames an extension. It's purely cosmetic and usually unnecessary. However in this case it's important to differentiate from the corecd
command.
Mark the bash script file executable using:
chmod a+x cd.sh
Now run the file:
$ cd $(./cd.sh)
bash: cd: /home/alex/Documents/A/B/C: No such file or directory
cd
we all know.
$(...)
executes command inside parenthesis and returns output.- If
cd.sh
was in your path you don't need to specify where it is. We prefix with./
to specify the command is in the current directory. - The
echo
output from thecd.sh
script is sent back to the parent via the$(...)
. The parent (our shell prompt) uses this output and passes it to the Linuxcd
command.
As others have mentioned a child process can't change the parent's directory. This is one way the child can tell the parent where to go after the process ends.
Because hello world is just a trace statement, let's try this:
Create bash script file cd.sh
containing:
#!/bin/bash
echo "/home/mike/Documents/A/B/C"
- The
.sh
extension is an older convention of giving bash script filenames an extension. It's purely cosmetic and usually unnecessary. However in this case it's important to differentiate from the corecd
command.
Mark the bash script file executable using:
chmod a+x cd.sh
Now run the file:
$ cd $(./cd.sh)
bash: cd: /home/alex/Documents/A/B/C: No such file or directory
cd
we all know.
$(...)
executes command inside parenthesis and returns output.- If
cd.sh
was in your path you don't need to specify where it is. We prefix with./
to specify the command is in the current directory. - The
echo
output from thecd.sh
script is sent back to the parent via the$(...)
. The parent (our shell prompt) uses this output and passes it to the Linuxcd
command.
As others have mentioned a child process can't change the parent's directory. This is one way the child can tell the parent where to go after the process ends.
edited Jul 24 at 23:01
answered Jul 24 at 22:08
WinEunuuchs2Unix
39.8k1066148
39.8k1066148
@wjandrea That's what you get for coding on a phone! Just got home, I'll fix it up. Thanks. Um just checked and it works fine. Maybe it's your14.04
version I read about an hour ago or so?
– WinEunuuchs2Unix
Jul 24 at 22:51
Well, you totally changed the script. Previously it was just one command:cd /home/mike/Documents/A/B/C
, which didn't produce any output. Now it'secho "/home/mike/Documents/A/B/C"
, which does produce output.
– wjandrea
Jul 25 at 1:01
@wjandrea True, I also totally changed the way of calling the script too. Instead of a simple./cd.sh
it's nowcd $(./cd.sh)
but it accomplishes the goal of the child changing the parent's current directory. Yes it's unconventional but it's another way of doing it that I hope people find interesting.
– WinEunuuchs2Unix
Jul 25 at 1:26
add a comment |
@wjandrea That's what you get for coding on a phone! Just got home, I'll fix it up. Thanks. Um just checked and it works fine. Maybe it's your14.04
version I read about an hour ago or so?
– WinEunuuchs2Unix
Jul 24 at 22:51
Well, you totally changed the script. Previously it was just one command:cd /home/mike/Documents/A/B/C
, which didn't produce any output. Now it'secho "/home/mike/Documents/A/B/C"
, which does produce output.
– wjandrea
Jul 25 at 1:01
@wjandrea True, I also totally changed the way of calling the script too. Instead of a simple./cd.sh
it's nowcd $(./cd.sh)
but it accomplishes the goal of the child changing the parent's current directory. Yes it's unconventional but it's another way of doing it that I hope people find interesting.
– WinEunuuchs2Unix
Jul 25 at 1:26
@wjandrea That's what you get for coding on a phone! Just got home, I'll fix it up. Thanks. Um just checked and it works fine. Maybe it's your
14.04
version I read about an hour ago or so?– WinEunuuchs2Unix
Jul 24 at 22:51
@wjandrea That's what you get for coding on a phone! Just got home, I'll fix it up. Thanks. Um just checked and it works fine. Maybe it's your
14.04
version I read about an hour ago or so?– WinEunuuchs2Unix
Jul 24 at 22:51
Well, you totally changed the script. Previously it was just one command:
cd /home/mike/Documents/A/B/C
, which didn't produce any output. Now it's echo "/home/mike/Documents/A/B/C"
, which does produce output.– wjandrea
Jul 25 at 1:01
Well, you totally changed the script. Previously it was just one command:
cd /home/mike/Documents/A/B/C
, which didn't produce any output. Now it's echo "/home/mike/Documents/A/B/C"
, which does produce output.– wjandrea
Jul 25 at 1:01
@wjandrea True, I also totally changed the way of calling the script too. Instead of a simple
./cd.sh
it's now cd $(./cd.sh)
but it accomplishes the goal of the child changing the parent's current directory. Yes it's unconventional but it's another way of doing it that I hope people find interesting.– WinEunuuchs2Unix
Jul 25 at 1:26
@wjandrea True, I also totally changed the way of calling the script too. Instead of a simple
./cd.sh
it's now cd $(./cd.sh)
but it accomplishes the goal of the child changing the parent's current directory. Yes it's unconventional but it's another way of doing it that I hope people find interesting.– WinEunuuchs2Unix
Jul 25 at 1:26
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f481715%2fwhy-doesnt-cd-work-in-a-shell-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Look at this Why doesn't “cd” work in a bash shell script?
– TuKsn
Jun 11 '14 at 8:45