Opening a terminal window to a specific directory from a bash script

Here is a small script I knocked up :

#!/usr/bin/osascript
on run argv
  set dir to quoted form of (first item of argv)
  tell app "Terminal" to do script "cd " & dir
end run

If you save this and make it executable

chmod +x script_filename

and then run it

script_filename ~/Desktop

then it will open up a new terminal window and change to the directory in the argument.


As of Mac OS X Lion 10.7, if you open a folder with Terminal it will create a new terminal at that location. e.g., you can drag a folder onto the Terminal application icon, or into a tab bar to create a new tab, and there are Services (New Terminal at Folder) you can use from the contextual menu to open a new window or tab for a selected folder in Finder or other applications (or even a pathname selected in text).

To do the equivalent from the command-line (or a shell script):

open -a Terminal /path/to/folder

This is the command-line equivalent of dragging the folder onto the Terminal application icon. (You can also supply a full path for Terminal if you want to specify a particular copy/version of the application.)