Find the answer to your Linux question:
Results 1 to 7 of 7
In bash: I know how to write a script as a login function that does a CD and "percolates up" to the "calling shell". Is there anyway to do this ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    5

    bash script file percolate cd up to calling shell

    In bash:

    I know how to write a script as a login function that does a CD and "percolates up" to the "calling shell". Is there anyway to do this in a script file so that the cwd (current working directory) is the last cwd of the called script?

    Note, I don't want to use the source builtins: 'source' and '. ' in the calling shell, and near as I can tell they can't be invoked from within the called shell script file, as in:

    . (
    ... called script
    )

    I sometimes use the above kind of structure for scripts that I want to execute asynchronously to the shell prompt, as in:

    (
    ... otherwise synchronous script
    ) &

    but it doesn't work with source.

    I've also tried 'export PWD' as the last statement in the called script, and that doesn't work either.

    TIA,

    Lee

  2. #2
    Just Joined!
    Join Date
    Sep 2007
    Posts
    5

    Thumbs up One man's cold broccoli is another man's moby hack! ;-)

    jozyba,

    You're "terrible hack" fit the bill perfectly because it not only works, but also makes clear to a reader of the "calling" script that the working directory is going to change based on what happens in the called script.

    I've actually used a technique similar to this (where the stdout of the script actually reports back to the calling script with some important side-effect information) before but for a completely different purpose.

    10Q>>much!,

    Lee

    P.S. OBTW, you've raised the level of answers I've seen here by 3 standard deviations in one swell foop!

  3. #3
    Just Joined!
    Join Date
    Aug 2007
    Posts
    37
    Ooops, sorry! I had second thoughts about my "horrible hack" and thought that I had deleted it before anyone had seen it. Anyway I'd better repost it now so that anyone else reading this thread knows what we're talking about......

    *****************************************

    I'm not sure that I've understood your question correctly. Do you mean something like this:

    The current working directory of calling shell is Directory-A
    The calling shell runs a script
    The script changes directory to Directory-B, then exits
    You want the current working directory of the calling shell to now be Directory-B


    If that's what you want to do then "officially" it can't be done because the script (child process) can't export its environment to its parent process (the calling shell).

    You can simulate passing the child's $PWD back to the parent by means of a really horrible hack. Say that the_script.sh did this:

    Code:
    #! /bin/bash
    cd /
    cd $HOME/Desktop
    echo $PWD
    exit 0
    Then your shell could call it like this:

    Code:
    cd "$(bash the_script.sh)"
    So the cd command would receive the output from the script's 'echo $PWD' command. The problem with this is that if the_script.sh has more than one 'echo' command in it then the output of all the echo commands will be passed to 'cd':

    Code:
    #! /bin/bash
    cd /
    echo "I'm now in the $PWD directory"
    cd $HOME/Desktop
    echo "And now I'm in the $PWD directory"
    echo $PWD
    exit 0
    The 'cd' command would choke on all that echoed output and would throw an error. So, to make a horrible hack even worse, you need to make sure that 'cd' only receives the last string that was echoed:

    Code:
    #! /bin/bash
    cd /
    echo "I'm now in the $PWD directory"
    cd $HOME/Desktop
    echo "And now I'm in the $PWD directory"
    echo xxx$PWD       #  Put 'xxx' or some other unique string in front of $PWD
    exit 0
    Now the echoed output can be extracted like so:

    Code:
    cd $(x="$(bash the_script.sh)"; echo "${x##*xxx}")

  4. #4
    Just Joined!
    Join Date
    Sep 2007
    Posts
    2

    easier

    Dag. You guys are making it too complicated.

    Here, this is easier.

    script (saved as cdbobdir, somewhere in your path):
    Code:
    #!/bin/bash
    cd /dir/you/want/to/go/to/bobdir
    pwd
    the, you just run this:

    Code:
    cd `cdbobdir`
    and you're there. The backticks execute the code inline and make cd use the output of cdbobdir.

  5. #5
    Just Joined!
    Join Date
    Aug 2007
    Posts
    37
    stuporglue, I don't think you've fully grasped the problem.

    Your solution:

    script (saved as cdbobdir, somewhere in your path):

    Code:
    #!/bin/bash 
    cd /dir/you/want/to/go/to/bobdir 
    pwd
    the, you just run this:

    Code:
    cd `cdbobdir`
    and you're there. The backticks execute the code inline and make cd use the output of cdbobdir.
    Is the same as my first solution:

    You can simulate passing the child's $PWD back to the parent by means of a really horrible hack. Say that the_script.sh did this:

    Code:
    #! /bin/bash 
    cd / 
    cd $HOME/Desktop 
    echo $PWD 
    exit 0
    Then your shell could call it like this:

    Code:
    cd "$(bash the_script.sh)"
    So the cd command would receive the output from the script's 'echo $PWD' command. The problem with this is that if the_script.sh has more than one 'echo' command in it then the output of all the echo commands will be passed to 'cd'.
    However, as I said earlier in this thread, if the script contains more than one echoed output:

    Code:
    #! /bin/bash
    cd /
    echo "I'm now in the $PWD directory"
    cd $HOME/Desktop
    echo "And now I'm in the $PWD directory"
    echo $PWD
    exit 0
    Then that solution will not work:

    Code:
    cd `cdbobdir`
    Gives an error message:
    Code:
    bash: cd: I'm: No such file or directory
    And that brings us back to my "horrible hack"....

  6. #6
    Just Joined!
    Join Date
    Sep 2007
    Posts
    2
    It's true that only your script will work if you are printing out other information in the mean time, but if you're writing a script which is to be used in the manner suggested (to change directories), why have it output something to the terminal?

    To the original poster: If you're just trying to write a command to change to a deep directory, you might just want to use an alias in your ~/.bashrc file. eg.

    alias cddeep="cd /home/steve/Documents/school/CS104/homework/project1/code"

  7. #7
    Just Joined!
    Join Date
    Sep 2007
    Posts
    5

    StuporGlue, if Only Bush had followed your lead

    and adopted the moniker BushLeague.

Posting Permissions

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