1

I have a large nested file structure; navigating from one working folder to the next requires verbose commands like cd ../../../../path/to/working/file. Fortunately, the files are consistently named: part01_Part01-04.fileName/src/main/ To make navigation easier, I've written a Python script that takes the current directory name and increments or decrements the part numbers according to command line args. The script prints an absolute filepath and then exits.

The Python script is called by a small Zsh function I wrote into my .zshrc, as follows:

function funcName () { builtin cd "$(/path/to/pythonExecutable $PWD $1)"; pwd; ls; }

Despite being a bit hacky, it works beautifully, as intended. My question has to do with the way it works: in order to have it work, I need to print the path string at the end of my Python script, as opposed to returning the path string. This was surprising and unexpected.

As I understand it, the return statement should deliver my string to whatever called the script, whereas print sends its cargo to stdout. My Zsh function is not stdout. Can anyone tell me why it is working this way, and/or point me to any resources to help me flesh out what's going on under the hood?

Last, I'm sure there are cleaner ways of doing this in shell script alone, but I know Python, and don't know shell (yet). One step at a time.

skytwosea
  • 249
  • 3
  • 8
  • 2
    No. The `return` statement returns a value from a Python function to another Python function. Your zsh function is EXACTLY reading the stdout file of that process. That's exactly what the `$(...)` construct does -- execute the enclosed process, read its stdout, and substitute the contents here. This is just how Unix pipelines work. – Tim Roberts May 28 '23 at 01:14
  • @TimRoberts Ok, copy that, and thanks. I'm reading up on command substitution, and on the return statement. – skytwosea May 28 '23 at 01:42
  • 1
    You can not "return" any data from a child process to the parent process. The Unix process model (which in the end is reflected in Linux, Solaris, MacOS and so on) allows data to go from parent to child, not from child to parent. Therefore you always need to simulate returning of data somehow - for instance by using stdout (as in your case), or by writing it to a file, which is then processed by the parent, or by using bidirectional pipes or sockets..... – user1934428 May 30 '23 at 11:34

0 Answers0