Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to create and run a bash function that:

  1. cd into a project's directory
  2. Open a new tab in Terminal in the same directory
  3. Open my dev tools and start dev services

Item #2 doesn't work as expected, the new tab doesn't change directory.

Here is the script (both functions residing in my .bashrc file:

# Open a new tab (needs a path as an argument)
    osascript -e "tell application \"Terminal\"" \
                        -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
                        -e "do script \"cd $1; clear;\" in front window" \
                        -e "end tell"
                        > /dev/null

# Set up workspace
    # cd into the project's directory
    cd /Users/iamuser/Documents/path/to/project\ file
    # Get the working directory

    # Open a new tab in Terminal and cd into project's directory
    # The idea is to have a tab with Rails server output, and another tab in the project's directory
    new_terminal_tab $pwd

    # Open the project in Sublime Text 2
    subl $pwd

    # Start the Rails server
    rails server

What am I doing wrong? What is this code doing that I don't know?


The path for the directory I am trying to access has spaces in it. But escaping the space is not helping at all. The script works with paths that do not include spaces.

share|improve this question

Opening a new tab in Terminal should by default retain the current directory. If it doesn't, you may've broken the $PROMPT_COMMAND.

Also remember to not replace the previous value if you intend to add custom behaviour by adding ; $PROMPT_COMMAND at the end.

PROMPT_COMMAND="my_custom_function; $PROMPT_COMMAND"

For reference, here's the default from /etc/bashrc (OS X 10.9):

# Tell the terminal about the working directory at each prompt.
if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
    update_terminal_cwd() {
        # Identify the directory using a "file:" scheme URL,
        # including the host name to disambiguate local vs.
        # remote connections. Percent-escape spaces.
        local SEARCH=' '
        local REPLACE='%20'
        local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
        printf '\e]7;%s\a' "$PWD_URL"
    PROMPT_COMMAND="update_terminal_cwd; $PROMPT_COMMAND"
share|improve this answer
$PROMPT_COMMAND is not broken. Any other suggestions? – sargas Apr 28 '14 at 17:43
up vote 1 down vote accepted

So as it was mentioned, my script worked with paths that didn't include spaces. It turns out that the solutions lies in wrapping the variable (inside new_terminal_tab()) in single quotes like so:

-e "do script \"cd '$1'; clear;\" in front window" \

Amazing what quotes can do isn't it?

share|improve this answer

The service provided here works for me. It opens a new Terminal tab at the folder I ran the service from.

I did encounter the problem that when I already had a Terminal window open it would open a new one. I solved it as follows:

on run {input, parameters}
    repeat with this_item in input
        set the_path to POSIX path of this_item
        repeat until the_path ends with "/"
            set the_path to text 1 thru -2 of the_path
        end repeat

        set numberOfWindows to "0"

        tell application "Terminal"
            set numberOfWindows to (numberOfWindows + (count windows))
            if numberOfWindows is 0 then
                do script with command "cd " & quoted form of the_path
                delay (0.3)
                tell application "System Events"
                    keystroke "t" using command down
                end tell
                do script with command "cd " & quoted form of the_path in window 0
            end if

        end tell
    end repeat
    return input
end run

All credit for this code goes to Jeroen Leenarts. I just made some small adjustments.

share|improve this answer
that's an interesting script. But my idea is to type a command in Terminal and have my applications opened as well as the two tabs in Terminal, one for Rails Server, another in the directory where the project resides. – sargas Apr 29 '14 at 15:45

I use an app called go2shell. This gives you a little button on Finder that opens the current directory in a terminal.

You can also use the terminal to cd to the folder you want, and use 'open .'

share|improve this answer
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – grgarside Apr 29 '14 at 5:20

Your Answer


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.