This question already has an answer here:

I have a script that I need to run an application which, at the end, changes the working directory. (I'm modifying an ".sh" script and am new to Unix.) I then need to run some commands in that new directory. I won't know what the directory will be before running the application. If I run the script as I would run things myself, the directory doesn't change until after I've run the entire script. I'd like to do this about a dozen times in a row so I'd rather not wait (sometimes hours) for each step to run another separate script myself.

Is there a way to run the application, have the directory change, then run additional work in that directory all in one go?

Edit: Here's the basics of my script. dset takes a parameter (ex. "10") and puts me in a directory. I create a subdirectory "../10" adjacent to it and move files to that new subdirectory.

. dset 10
mkdir ../10
mv tmp.out ../10

marked as duplicate by l0b0, Christopher, roaima, Anthony Geoghegan, Archemar Mar 23 '18 at 12:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Can you post your script? – L.Ray Mar 21 '18 at 18:06
  • Added it. @L.Ray – VenerableAgents Mar 21 '18 at 18:27
  • 1
    Long story short - you can't cd in a script unless you're sourcing it. – l0b0 Mar 22 '18 at 20:26
  • So, there's no way to run a series of scripts or something? I have to manually start the next script once the directory changes? – VenerableAgents Mar 23 '18 at 15:15
  • This post got marked as a duplicate. I don't think it's a duplicate of that question. I had found that question and it deals with "cd" which my script doesn't do. It's the application I call that changes the directory. If I ran this script manually in the prompt, it would change directory after the first line... as it runs in a script, it changes directory at the end of the script run. – VenerableAgents Mar 23 '18 at 15:18

Browse other questions tagged or ask your own question.