Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a script that i want to create files, depending on the directory the user (me) is in (it can be called from command line). However, it creates the file in the same directory the script is located. How can I change the script so it creates the file in the same directory as the terminal?

Note: The command pwd when executed inside the script will print the working directory of the script, not the terminal.

share|improve this question
1  
can you give an example of "the working directory of the terminal"? –  Minh Danh Nov 2 '12 at 3:38
add comment

1 Answer

Are you changing directories inside your script? In bash and in the script, I think pwd actually should be returning the working directory of that user, not the location of the script itself.

e.g.

echo "touch make_a_file" > make_a_file.sh
~/ $ ls
make_a_file.sh
~/ $ mkdir -p make/some/directories
~/ $ cd make/some/directories
~/make/some/directories $ sh ../../../make_a_file.sh
~/make/some/directories $ ls
make_a_file

as you can see here, from ubuntu 12.10 with the current version of bash, I created a script, ran it from a different directory (from inside the make/some/directories path), and the new file was created within the directory I was in... not the location of the script. Be sure you're not moving anywhere in your script or show some code to see what is going on.

share|improve this answer
    
I think what the problem was was that it was a script executing another script... All the script does is run two lines of code, touch and chmod creating the file then giving it +x perms. I think I might not bother with it as it's not that big a deal... –  KILL3RTACO Nov 2 '12 at 4:41
    
@KILL3RTACO: Then the other script must be changing directories. Whenever you launch a script, it always inherits the current working directory. The only way for the working directory to change is for the script to change it explicitly. –  Scott Severance Nov 4 '12 at 6:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.