Skip to contentSkip to conversation

BASH Script Change Directory

DISCUSSION Bob Swift 2 participants 3 messages May 2, 2003

lore
Share & tools
lore

Bob Swift

author
#0 lore raw

This is going to sound like a really dumb question, but is there any way to have BASH leave the user in the current directory from the script, rather than returning them to the directory they were in when the script was called?

For example... the user is in their home directory and call the following BASH script:

     #!/bin/bash
     cd /var/log
     ls

When the script exits, the user is returned to their home directory. Is there a way to leave them in the /var/log directory?

I've tried the obvious things like:

trap 'cd /var/log' 0

with no success. I'm guessing that there's probably some really simple thing that I'm missing.

Thanks.

Signature · 7 lines
Bob Swift

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#1 lore raw
Quoted 7 lines · Bob Swift

This is going to sound like a really dumb question, but is there any way to have BASH leave the user in the current directory from the script, rather than returning them to the directory they were in when the script was called?

For example... the user is in their home directory and call the following BASH script:

Quoted 7 lines
     #!/bin/bash
     cd /var/log
     ls

When the script exits, the user is returned to their home directory. Is there a way to leave them in the /var/log directory?

The script runs in a subshell. What you can do in this case is

source your-script' or just . your-script'

Ken

Signature · 6 lines
Live Long or Prosper! No, wait, that wasn't it...
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
author
#2 lore raw
Quoted 1 line · Ken Moffat

The script runs in a subshell. What you can do in this case is

Okay, I understood the subshell part, and figured that's what was resetting everything.

Quoted 1 line

source your-script' or just . your-script'

That's what I was missing! I had completely forgotten about the "source" command. I also learned something because I didn't know about the "dot" trick.

Thanks.

Signature · 7 lines
Bob Swift

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs