Shell script to change directory of terminal and open a second terminal

11,865

Solution 1

Your first version first opens a gnome-terminal, waits until you close it and then changes to the new directory.

Your second version tries to run a command cd instead of a shell, however cd is not a real command but a shell builtin. (See type -a cd for that.)

The question is how gnome-terminal decides what directory to display. It will normally use the current working directory unless overridden by the --working-directory option.

Therefore you can use either:

cd /path/to/dir && gnome-terminal

or

gnome-terminal --working-directory=/path/to/dir

Have a look at man gnome-terminal for available options.

For the ssh part you have to decide whether you want to run you gnome-terminal on the local or remote side. To run it on the remote site you use:

ssh -X [email protected] gnome-terminal --working-directory=/path/to/dir

for the local side you can use something like:

gnome-terminal -e "ssh -t [email protected] bash -c 'cd /path/to/dir && bash -l'";

Solution 2

The working directory would stay changed if you ran the script with a dot (space) before the name of the script.

For instance, I use one named pj which moves me into my python directory for projects. It contains these two lines:

#!/bin/bash
cd /home/pi/python

I made it executable then copied it to /usr/sbin where it is on my $PATH.

To run it, I type:

. pj         (note the dot and the space)

And voila, it changes into that directory and leaves me there.
Couldn't be easier.

Share:
11,865

Related videos on Youtube

bs7280
Author by

bs7280

Updated on September 18, 2022

Comments

  • bs7280
    bs7280 almost 2 years

    I am new to writing shell scripts and I am trying to write a script to CD to a folder where my programs are saved for one of my classes, then have a second terminal open that ssh's to a server used for testing the programs that I write. The problem is that I can not find a way to change the directory of the terminal shell to be where my programs are stored.

    #!/bin/bash/
    gnome-terminal -e "ssh [email protected]";
    cd /path/to/dir
    

    the problem is that the working directory does not change after the script terminates (it stays as it was when the script was called). I tried calling it except with a '.' in front of the name of the script to get it to run in the current process but the second terminal never opened.

    I tried to do the same thing as above except I replaced the third line with

    gnome-terminal -e "cd /path/to/dir/";
    

    so that way two terminals would open, but the terminal ment to CD to the path gave an error along the lines of "there was an error creating the child process for this terminal: failed to execute child process 'cd'"

    can anyone help me figure out how to solve this problem?

  • bs7280
    bs7280 over 9 years
    I think you misunderstand the question. I want to have two separate terminals. One where the working directory changes to a given folder, and another that is ssh'd into a remote server that is used to do testing
  • michas
    michas over 9 years
    for two different gnome-terminals just run it twice. gnome-terminal --working-directory=/path/to/dir & gnome-terminal -e "ssh [email protected]" the first one starts a gnome-terminal at the given path and the second one starts a terminal which connects to the remote host.
  • bs7280
    bs7280 over 9 years
    Thank you, this should solve my problem when I write it out. But for the sake of learning: how could I change the directory of the terminal that the file was called from?
  • bs7280
    bs7280 over 9 years
    Actually I just tested it and the new terminal's working directory is just my default/home folder.
  • Michael Homer
    Michael Homer over 9 years
    Use an alias or a function (see the linked related questions).
  • michas
    michas over 9 years
    A shell script (or any other called command) will run as a separate process and is therefore not able to change the directory of the calling shell. You need to source your script, use an alias or a function to do such things.
  • Jonathan Hartley
    Jonathan Hartley almost 8 years
    gnome-terminal --working-directory no longer works in Ubuntu 16.04 because of a bug: bugs.launchpad.net/ubuntu/+source/gnome-terminal/+bug/158715‌​4 Go there and click 'This affects me too' if you'd like a fix.