Results 1 to 5 of 5

Thread: Basic Script

  1. #1
    Join Date
    Jun 2011
    Beans
    29

    Basic Script

    I'm almost embarassed to ask this but I'm stuck. I have, within my home directory a directory called work and then with in that my company, and with in there a directory for each project.

    $HOME/work/mycompany
    project-a
    project-b
    ...
    project-n

    I've created a script (if you can even call it that!) and put it in my $HOME/bin folder. THe idea is that I will be able to type mycompany project-x from anywhere in the terminal and have my directory location changed to the work directory. My script has this right now ---

    Code:
    #!/bin/bash
    cd $HOME/work/mycompany
    .. with the intention of trapping the arguments once I see this working. I put some echo statements in originally to make sure that it was running, which it is, however, I never change location -- the script ends and I am still where I was when I ran it.

    What am I missing -- i know it's something simple but I can't see it.

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Basic Script

    It's because the script runs in a subshell.

    You can source the script instead of running it as a child:
    Code:
    . yourscript
    Or you could simply write a little function or an alias and put it in your ~/.bashrc file.

  3. #3
    Join Date
    May 2008
    Location
    0100110101101001
    Beans
    1,229
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Basic Script

    Try this:

    Code:
    #!/bin/bash
    cd /HOME/work/mycompany
    We have no Great War, no Great Depression. Our Great War is a Spiritual War. Our Great Depression is our lives. - Tyler Durden

  4. #4
    Join Date
    Jun 2011
    Beans
    29

    Re: Basic Script

    Quote Originally Posted by sisco311 View Post
    It's because the script runs in a subshell.

    You can source the script instead of running it as a child:
    Code:
    . yourscript
    Or you could simply write a little function or an alias and put it in your ~/.bashrc file.
    That worked. Thanks Sisco311.

  5. #5
    Join Date
    Apr 2009
    Location
    Midwest, U.S.A.
    Beans
    1,209
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Basic Script

    Another elegant solution is to add the appropriate pathname to the variable 'CDPATH' in your ~/.bashrc file.
    Code:
    CDPATH="$CDPATH:$HOME/work/mycompany"
    And, that way you can simply 'cd' into any project directory!
    Laptop: Dell Inspiron 8200 - Fedora 13 - Goddard
    Desktop: Self-Built - [Ku, Lu, Xu, U]buntu - Lucid 10.04.3 (LTS)
    Linux User: 498249 / Ubuntu User: 29241

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •