Results 1 to 3 of 3

Thread: Two scripts: One changing directory other not?

  1. #1
    Join Date
    Jun 2015
    Beans
    33

    Two scripts: One changing directory other not?

    I have two scripts. One is :
    Code:
        #!/bin/bash
        if [ $1 = 1 ]; then 
           dir=mydir-1.6_
        else
            dir=mydir
        fi
        cd ~/code/${dir}$2
        echo $(pwd)
    The above script changes directories even though there are several posts that say that since a script is run in a sub-shell it should have no effect on the executing shell.


    Now I have another script:


    Code:
      #!/bin/bash
        dir=/WORK/temp/$1
        mkdir -p $dir
        cd $dir
        wget http://somurl.com/archive.zip
        unzip archive.zip
    The above script unzips the file in the expected directory but leaves the calling shell in the same directory. What is the difference when cd is called in both scripts?

  2. #2
    Join Date
    Aug 2011
    Location
    51.8° N 5.8° E
    Beans
    4,874
    Distro
    Xubuntu 19.10 Eoan Ermine

    Re: Two scripts: One changing directory other not?

    Does the first really put the calling shell in a new directory? The echo $(pwd) is running in the subshell, so that reports the new directory, but after the script terminates you should be back in your old shell and in your old directory.

    If you don't run the stript as in ./script or bash script, but instead source it as in source script or . script, then the script will execute in the current shell, which will change your current directory in the calling shell.

  3. #3
    Join Date
    Jun 2015
    Beans
    33

    Re: Two scripts: One changing directory other not?

    Quote Originally Posted by Impavidus View Post
    Does the first really put the calling shell in a new directory? The echo $(pwd) is running in the subshell, so that reports the new directory, but after the script terminates you should be back in your old shell and in your old directory.

    If you don't run the stript as in ./script or bash script, but instead source it as in source script or . script, then the script will execute in the current shell, which will change your current directory in the calling shell.
    Here is a demo

    Code:
    salilsurendran@salilsurendran-ThinkPad-P50:~$ myscript 1 4
    /home/salilsurendran/code/mydir-1.6_4
    salilsurendran@salilsurendran-ThinkPad-P50:~/code/mydir-1.6_4$ pwd
    /home/salilsurendran/code/mydir-1.6_4
    salilsurendran@salilsurendran-ThinkPad-P50:~/code/mydir$ myscript 2 3
    /home/salilsurendran/code/mydir3
    salilsurendran@salilsurendran-ThinkPad-P50:~/code/mydir3$ pwd
    /home/salilsurendran/code/mydir3
    salilsurendran@salilsurendran-ThinkPad-P50:~/code/mydir3$

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
  •