1

I am currently doing the MIT missing semester lecture 2.

Exercise number 2 has us write functions marco.sh and polo.sh to "polo" back to the directory where marco was executed. I wrote two simple scripts for this, but polo.sh does not cd into the directory I saved.

marco

#!/bin/bash

currentDirectory=$(pwd)

echo "current directory is $currentDirectory"

echo "$currentDirectory" > /tmp/missing/marcoDirectory.txt

(it was necessary to save the directory in an .txt file because bash wouldn't recognize the currentDirectory variable with polo. Again, this is an exercise, I'm not super proud of it)

polo

#!/bin/bash

next=$(cat /tmp/missing/marcoDirectory.txt)

echo "changing directory to $next"

cd $next

The fact is that the command cd does not seem to recognize that it has to be executed. If I try to cd somewhere else, it also fails.

How should I approach solving the cd issue?

Community
  • 1
  • 1
  • 1
    How do you find out that switching to the directory did not work? Do you assume that you will be in the `$next` directory when the script ends? – Cyrus Jun 05 '20 at 00:00
  • Yes, I assumed that my terminal window would be at the `$next` on the end. I found out that it didn't work because I was still at the same directory after running them both. For instance, if I'm at /sodium/lithium I can run marco.sh and it's saved in the .txt file. Then, let's say I cd my way to /carbon/nitrogen. If I run polo.sh, it echoes "changing directory to /sodium/lithium", but keeps at /carbon/nitrogen. –  Jun 05 '20 at 01:07
  • 2
    Scripts normally run in subprocesses, meaning that changing directory (and setting variables) changes the state of the subprocess, but has no effect on the parent process. To affect the parent process, you need something like a function (*not* a script), or a script you run with `.` or `source` instead of a normal script. See: ["Change the current directory from a Bash script"](https://stackoverflow.com/questions/874452/change-the-current-directory-from-a-bash-script) – Gordon Davisson Jun 05 '20 at 01:49
  • **polo** always runs from `/tmp/missing` ? To ensure that **polo** will work, you should `cat /tmp/missing/marcoDirectory.txt`. – Nic3500 Jun 05 '20 at 02:32
  • You are not writing bash functions. You are writing bash scripts. If you write functions instead of scripts (as per the exercise instructions), then it will work fine. – that other guy Jun 05 '20 at 04:05
  • Thank you for those kind users that share detailed, useful comments. However, as you all can see, this is an elementary question. That is why concise comments that assume higher knowledge of bash scripting are not helpful at all. Thank you for refraining to add such comments. –  Jun 05 '20 at 13:26
  • Do not blame the people that are trying to help if you do not understand the comments. Look at @GordonDavisson's link. – Nic3500 Jun 07 '20 at 01:29
  • Some people really do help, and I'm grateful for it. Gordon's comment was one of the best, I only had no time to implement it. However, it seems that not everyone in stackoverflow wants to build a beginner-friendly environment, and I don't have time to waste with them. –  Jun 08 '20 at 16:53

1 Answers1

0

marco.sh could be a function:

function marco(){
currentDirectory=$(pwd)

echo "Current directory is $currentDirectory"
echo "$currentDirectory" > /tmp/missing/marcoDirectory.txt
}

then you could source marco.sh and run marco.sh.

polo.sh could be smth like:

function polo(){
    next=$(cat /tmp/missing/marcoDirectory.txt)
    echo "Changing directory to $next"
    cd "$next"
}

Then it should redirect to marco directory. Note that we should have "" for next variable.

IO8
  • 1