Home > Blockchain >  How exactly does $PATH work in Terminal on Macs and how exactly does .zshrc file influence it?
How exactly does $PATH work in Terminal on Macs and how exactly does .zshrc file influence it?

Time:05-07

I'm new to programming, to Macs and I'm having a hard time wrapping my head around how Terminal works in relation to $PATH and the .zshrc file.

To preface this somewhat: I'm working on a Macbook Pro 14 running the M1 Pro, and am trying my hand at programming. I've been going through the Flutter installation and setup process and at some point, the tutorial I'm following has told me to declare the path of my Flutter SDK by adding that path to my .zshrc file. And so I did, as so:

export PATH=$HOME/dev/tooling/flutter/bin:$PATH

I saved the file and restarted Terminal and tried calling flutter doctor and it works - fantastic, my arms are happy-flailing in the air. Commenting out the path added means I can't call it in Terminal anymore by just typing flutter when I'm working outside of the .../flutter/bin directory - makes sense to me.

Somewhere a little further down the tutorial, I'm instructed to install Java, so I do. I install Azul's Zulu Java for Macs running on ARM architecture and again, I'm told to declare the path to Java in my .zshrc file as so:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-18.jdk/Contents/Home

I do it I call java and try calling java -version and they both work.

Now, the problem begins from the next step

In the next step I am told to install Android Studio, which I do. I then go back to Terminal and run flutter doctor and it highlights Android toolchain as an issue, expanding to say that cmdline-tools component is missing. I do a search and find a suggested solution about ticking Android SDK Command-line Tools in Android Studio > System Settings > Android SDK > SDK Tools, followed by declaring two folders named tools and platform-tools. I do as instructed but ultimately, I end up declaring it with the wrong syntax and on top of that, realised that I didn't click apply when I ticked the Android SDK Command-line Tools in Android Studio. So then my .zshrc file looked like this:

export PATH=$HOME/dev/tooling/flutter/bin:$PATH

export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-18.jdk/Contents/Home

export PATH=$HOME/Library/Android/sdk/tools
export PATH=$HOME/Library/Android/sdk/platform-tools

Immediately after I saved the .zshrc file and restarted Terminal, none of the commands were recognised. Not flutter nor flutter doctor nor java or java -version; or even vim ~/.zshrc. I scratch my head, reopen the .zshrc file and comment out the last two newly added lines, restart Terminal and voila, it's all okay again.

To add to the confusion, I then edit my .zshrc file to look like this (the second line of code was intentionally written in what I now knew was incorrect syntax to see what affects it'd have):

export PATH=$HOME/dev/tooling/flutter/bin:$PATH

export PATH=$HOME$PATH:$HOME/Library/Android/sdk/tools
#export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-18.jdk/Contents/Home

#export PATH=$HOME/Library/Android/sdk/tools
#export PATH=$HOME/Library/Android/sdk/platform-tools

and calling flutter and flutter doctor doesn't work; however calling java and java -version does (which leads me to believe that my line declaring a path for java is pretty pointless). But then when I re-edited my .zshrc file to look like this:

export PATH=$HOME/dev/tooling/flutter/bin:$PATH

#export PATH=$HOME$PATH:$HOME/Library/Android/sdk/tools
#export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-18.jdk/Contents/Home

export PATH=$HOME/Library/Android/sdk/tools
export PATH=$HOME/Library/Android/sdk/platform-tools

none of the commands work - not flutter nor flutter doctor or java etc.

What's the deal here? I'm pretty confused. I'm guessing it has something to do with the positioning of $PATH in the active lines of code but I can't quite figure it out.

It's a long read and likely a very silly question but it will eat at me if I can't get to the bottom of it lol. Thanks in advance!

CodePudding user response:

When you do a

export PATH=$HOME/Library/Android/sdk/tools

you destroy the old setting (i.e. $HOME/dev/tooling/flutter/bin:$PATH). I would write instead

path =(~/Library/Android/sdk/tools)

which would append the new directory to the existing PATH.

  • Related