0

I am writing a bash script that moves an existing directory to a new name, creates a new directory with the old name and then changes into that newly created directory. Strangely, the new directory is not empty but contains everything from the old, now renamed, directory. But the move was succesfull, the renamed directory exists and contains what it shall contain. And if I compare inodes, I'm still in the renamed directory, but the name indicated with 'pwd' shows that I should be in the new directory.

To make things more clear, I'm starting off in the directory I want to create a backup from. The script essentially does the following:

cd <away>
mv <olddir> <olddir>~
mkdir <olddir>
cd <olddir>

I am using full pathnames; using pushd/popd gives the same result. The result is very strange (test9 is an empty file I created to easily see if I'm in the expected directory):

/opt/camera_pkg_sdk$ ls -ia1
20578307 .
20447233 ..
20578305 test9
/opt/camera_pkg_sdk$ ls -ia1 ../camera_pkg_sdk
20578306 .
20447233 ..
/opt/camera_pkg_sdk$

Inode 20578307 is the renamed directory, 20578306 is the new one. I tried inserting 'sync' in various locations in my script, without any luck. If, on the command line after the script has run, I manually go up one level and then back, all is normal again, and I see the correct inode numbers.

Any idea what to try? How to work around? What is the reason? Or even have a true solution? I am as interested in the why it happens, as in a working way to achieve what I need.

4
  • 1
    Please try to clarify. Very long story, and I do not get the point after a few rereads.
    – vanadium
    May 19, 2021 at 10:12
  • My script executes the pseudo code listed, and the final cd goes into the wrong directory. @vanadium May 19, 2021 at 10:23
  • Where is <away> located wrt <olddir>?
    – muru
    May 19, 2021 at 10:24
  • I use root, ‘/‘, to just get out of the way. May 19, 2021 at 10:27

1 Answer 1

1

You never changed directory until...

If, on the command line after the script has run, I manually go up one level and then back, all is normal again.

You cannot change the current directory of a terminal by running another script in the usual way. This is because scripts are executed in a subshell which has no effect on the environment of the parent shell, i.e. your terminal session.

You need to source your script as:

source script

which can also be abbrieviated as:

. script

to run it in within the same shell.

2
  • You mean that in the shell running my script all is as intended, but my bash shell doesn't really notice until I manually change directories, correct? My script is a lot more than just this part, and I hesitate to source it, as long as the correct commands are executed on the intended files and directories. May 19, 2021 at 12:23
  • 1
    Yes. The parent was still in the same directory, which it had no reason to know had moved since when it had originally opened it. May 19, 2021 at 12:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .