1

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.

1
  • 1
    can you give an example of "the working directory of the terminal"?
    – Minh Danh
    Nov 2, 2012 at 3:38

1 Answer 1

4

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.

2
  • 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, 2012 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. Nov 4, 2012 at 6:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .