mac – system PHP + android platform tools | adding to zsh PATH *




Android Platform Tools


#GET INFO

brew info android-platform-tools


My Homebrew installation (brew install --cask android-platform-tools) use this Path:
==> Artifacts
/usr/local/Caskroom/android-platform-tools/33.0.1/platform-tools/

/usr/local/Caskroom/android-platform-tools/30.0.0/platform-tools

You would be better off using the symlink that Homebrew creates to ANDROID_HOME, as it will always be the newest version:

/usr/local/opt/android-sdk/

So your PATH would be:

export PATH=$PATH:/usr/local/opt/android-sdk/tools:/usr/local/opt/android-sdk/platform-tools




SYSTEM PHP



brew install php@7.4 (max for NATS)
Install the required PHP to your PATH
also if you are using oh-my-zsh update path in your .zshrc export PATH="/usr/local/opt/php@7.3/bin:$PATH" and restart terminal – 

Then make sure it's all working

php -v
php --version


This command will show you where your ini file is loaded
php --ini

cleanup steps


brew update && brew upgrade
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew unlink php@56  
brew install php@71
You might get an error if PHP 5.6 has not been installed by brew previously, but don't worry, you can simply continue.
You can also change the version to 7.0 by replacing the command from above commands from brew install php@71 to brew install php@70.
You can check the output by command.

php -v
If the output of php -v still doesn’t echoes the version 7, simply type this command and hit enter in terminal.

UPDATE PATH
export PATH=/usr/local/php5/bin:$PATH
While I was able to upgrade and install via this method it still showed as php 7.1 until I did the last export PATH step, so kudos for that! Also I don't know if anything changed but I had to run the install script as brew install php@7.3 with a dot between major/minor versions –

brew install php@7.4 –

Because it is now versioned


php@7.4 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have php@7.4 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@7.4 you may need to set:
  export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
  export CPPFLAGS="-I/usr/local/opt/php@7.4/include"


To restart php@7.4 after an upgrade:
  brew services restart php@7.4
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/php@7.4/sbin/php-fpm --nodaemonize
==> Summary
🍺  /usr/local/Cellar/php@7.4/7.4.29: 498 files, 72.4MB
==> Running `brew cleanup php@7.4`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> php@7.4
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so

    
        SetHandler application/x-httpd-php
    

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.4/

php@7.4 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have php@7.4 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@7.4 you may need to set:
  export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
  export CPPFLAGS="-I/usr/local/opt/php@7.4/include"


To restart php@7.4 after an upgrade:
  brew services restart php@7.4
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/php@7.4/sbin/php-fpm --nodaemonize



https://stackoverflow.com/questions/11530090/adding-a-new-entry-to-the-path-variable-in-zsh/18077919#18077919

 

Adding a new entry to the PATH variable in ZSH

I'm using zsh terminal, and I'm trying to add a new entry (/home/david/pear/bin) to the PATH variable. I don't see a reference to the PATH variable in my ~/.zshrc file, but doing echo $PATH returns:

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

So I know that the path variable is being set somewhere. Where is the PATH variable set / modified for the zsh terminal?

linuxubuntuzsh

In my opinion, PATH should be manipulated in .zshenv, not in .zshrc...

Rmano

https://stackoverflow.com/questions/11530090/adding-a-new-entry-to-the-path-variable-in-zsh/18077919#comment113414524_11530090)

In case anyone else is curious about @Rmano's pointer on using '.zshenv' (as I was), here's link a detailed discussion.

YCode

Jul 4, 2021 at 12:53

 

8 Answers

Sorted by: Highest score (default)

 

Actually, using ZSH allows you to use special mapping of environment variables. So you can simply do:

# append
path+=('/home/david/pear/bin')
# or prepend
path=('/home/david/pear/bin' $path)
# export to sub-processes (make it inherited by child processes)
export PATH

For me that's a very neat feature which can be propagated to other variables. Example:

typeset -T LD_LIBRARY_PATH ld_library_path :

 

Nice answer. In my case, ~/.zshrc is sourced after .profile, and overwrites everything in .profile. Took a while pulling my hair to figure it out.

Khanh Nguyen

Jun 16, 2014 at 23:53

 

The append case does does not need the parens unless you're appending more than one element. It also often doesn't need the quotes. So the simple, short way to append is

Micah Elliott

Jun 11, 2015 at 21:54

@SuperUberDuper, you should understand that almost any unix shell simply reads startup files which does almost the same as if you'd type it into shell interactively. Regarding "rc" files you might find interesting answer to this question

ony

Mar 7, 2016 at 9:08

    It's possible to avoid explicit export with -x and leave only unique values in a variable with -U, colon is assumed by default, so it can be: typeset -TUx PATH path

    Grief

    Mar 16, 2017 at 19:53

  • 1

    I see the use of path and PATH, both are separate entities?

    CousinCocaine

    Nov 7, 2019 at 15:09

  • @CousinCocaine, you are right. ZSH variables are case-sensitive. typeset -T allows to tie PATH and path together in a special way that internally ZSH manages array variable path and reflects its contents in PATH as a scalar.

    ony

    Nov 17, 2019 at 22:53

  • How do I set exactly what PATH should be, as opposed to appending to it? Because every time .zshrc is executed, PATH gets whatever is declared in .zshrc appended to it again and again.

    Daniel Springer

    Nov 18, 2019 at 0:00

  • @DanielSpringer, there is a hint in second example. By just writing path=(...) you get it set. You might be interested as well in comment from @Grief about -U. And I'd recommend modifying PATH environment from ~/.*profile-like files so they will not execute when you run inner shell. See also something like shreevatsa.wordpress.com/2008/03/30/… or other similar tips.

    ony

    Nov 24, 2019 at 20:04

  • It seems that "path=" appends to path rather then setting it. What I didn't know is that path is reset upon relaunching Terminal/Shell/(maybe?)Login session

    Daniel Springer

    Nov 26, 2019 at 2:23

  • 3

    @DanielSpringer, no. If you want it in those terms then: path=(...) (without referencing $path or $PATH inside) assigns, path=(... $path) prepends and path+(...) appends.

    ony

    Dec 1, 2019 at 7:18

  • You can even have it as 1-liner: export path=(... $path).

    caram

    Jan 5, 2020 at 10:15

  • This doesn't work for me on my mac. I use the one liner answer below that updates ~/.zshrc insead.

    Randy

    May 5, 2020 at 18:02

  • @KhanhNguyen how can I see when these files are sourced? Is there a cmd?

    Timo

    Dec 31, 2020 at 10:09

  • 1

    @Timo, in case of zsh you can try something like zsh -x -i -l -c true to see all commands during shell start-up. That -l is to simulate login-like start (as it happens from text VT or over ssh). But you should understand that not always zsh will be started as login (e.g. graphical session) and some other parent process may somehow source .profile before spawning non-login zsh sub-process that inherits environment variables (including PATH) from parent process.

    ony

    Jan 1, 2021 at 18:37

  • 1

    What if i want to use $HOME inside ' '? path +=('$HOME/etc/etc') doesn't seem to work

    Rotkiv

    Jan 27, 2021 at 22:02

  • 1

    @Rotkiv, that's a bit different question about how single/double quotes are interpreted. I guess zsh expansion covers this. Usually double-quotes allow string interpolation like "$HOME/etc/etc", but single quotes makes it easier to represent text that should not get special treatment. Note that you can always mix in a single argument forms to have something like "$HOME"'/etc/etc' (double + single quotes) or "$HOME"/etc/etc (double quotes + unquoted). P.S. Yes I'm lazy to try find link to good answer on SO 😛

    ony

    Jan 31, 2021 at 21:45

  • On macOS, you can prepend and append to your path variable as described in the post, adding to the .zshrc file. It works without exporting the PATH.

    Jost

    Mar 27, 2021 at 13:46

  • If this append or prepend syntax doesn't work for you, make sure that you are using lower case path. Lower case path is the array.

    Yerke

    Jun 17, 2021 at 3:35

  • Thank you for showing the prepend as well! Didn't see it anywhere else.

    MEMark

    Aug 21, 2021 at 9:38

  • This does not seem to work with ~.

    Minh Nghĩa

    Jan 14 at 6:54

  • This didn't work for me even though things looked absolutely fine when I echo $PATH. I had to go back to the "traditional" syntax of something like: export PATH=$ANDROID_HOME/platform-tools:$PATH

    Phil

    Jan 20 at 21:55

  • 1

    @MinhNghĩa, ~ is expanded by shell. If you put it in quotes like '~/bin' or "~/bin" it will be literally tilda in your path. E.g. you can write path+=(~/bin) (note no quotes) or similar. If you need to quote something, you still can do it on part like path+=(~/'some path with spaces').

    ony

    Jan 22 at 17:57

  • @Phil, my best guess for your case is that ok result of echo $PATH shows your current shell value for it. Any chance it works if you do export PATH (upper case) to export for sub-process after setting path (lower case)?

    ony

    Jan 22 at 18:01

Here, add this line to .zshrc:

export PATH=/home/david/pear/bin:$PATH

EDIT: This does work, but ony's answer below is better, as it takes advantage of the structured interface ZSH provides for variables like $PATH. This approach is standard for bash, but as far as I know, there is no reason to use it when ZSH provides better alternatives.

 

Here, add this line to .zshrc:

export PATH=/home/david/pear/bin:$PATH

EDIT: This does work, but ony's answer below is better, as it takes advantage of the structured interface ZSH provides for variables like $PATH. This approach is standard for bash, but as far as I know, there is no reason to use it when ZSH provides better alternatives.


 

    haha forget it, I though that was only a console command but adding that line to the .zshrc did the trick. Thanks a lot!

    David Barreto

    Jul 17, 2012 at 20:42

  • 1

    My .zshrc already had a line for export PATH so I replaced it with the modified one.

    Zack Huston

    Feb 27, 2014 at 13:32

  • 6

    I had to remove the double quotes around the entries i.e. PATH="/home/david/pear/bin:/usr/bin:etc" to PATH=/home/david/pear/bin:/usr/bin:etc for it to stay in zshrc.

    a7omiton

    Feb 7, 2015 at 15:01

  • 4

    @taco, you can use $HOME

    mencargo

    Nov 10, 2015 at 1:38

  • It will erase all the old PATH, try export PATH=$PATH:/path/to/dir

    sstruct

    Jan 19, 2017 at 3:10

  • This appends to my existing PATH, and every time it's run, appends again. How do I set exactly what PATH should be?

    Daniel Springer

    Nov 17, 2019 at 23:59

  • You can set PATH directly @DanielSpringer: export PATH=/some/where:/bin:/usr/bin and so on. Is that what you're asking? The idea is to run the code in this answer in the .zshrc or .profile or something like that at the beginning of each session.

    Linuxios

    Nov 18, 2019 at 0:01

  • 1

    @DanielSpringer: Yes. When you open a shell it inherits PATH from the parent process that started it, and then when it runs .zshrc (or .bashrc or whatever), that's what lets you add extra things to that path.

    Linuxios

    Nov 18, 2019 at 0:04

  • @DanielSpringer: Worth noting that you have to add the folder they're in to your path, not the path to the script file itself.

    Linuxios

    Nov 18, 2019 at 0:04

  • @Linuxios Ah, and do I need to add the path to the file itself too, or just its folder?

    Daniel Springer

    Nov 18, 2019 at 0:05

  • Just the folder. PATH is a list of places (folders; directories) where the shell will look for commands you try to run. For example, when you run wget ... the shell is looking for and finding the wget binary in /usr/bin, for example, which is in the default PATH.

    Linuxios

    Nov 18, 2019 at 0:07

  • Just a side note, if your path has a space in it. then you need to wrap your path in double quotes like this: export PATH=$PATH:"/Applications/Android Studio.app/Contents"

    Hamza Waleed

    Jun 9, 2021 at 9:35

     


ANSWER 2

You can append to your PATH in a minimal fashion. No need for parentheses unless you're appending more than one element. It also usually doesn't need quotes. So the simple, short way to append is:

path+=/some/new/bin/dir

This lower-case syntax is using path as an array, yet also affects its upper-case partner equivalent, PATH (to which it is "bound" via typeset).

(Notice that no : is needed/wanted as a separator.)

Common interactive usage

Then the common pattern for testing a new script/executable becomes:

path+=$PWD/.
# or
path+=$PWD/bin

Common config usage

You can sprinkle path settings around your .zshrc (as above) and it will naturally lead to the earlier listed settings taking precedence (though you may occasionally still want to use the "prepend" form path=(/some/new/bin/dir $path)).

Treating path this way (as an array) also means: no need to do a rehash to get the newly pathed commands to be found.

Also take a look at vared path as a dynamic way to edit path (and other things).

You may only be interested in path for this question, but since we're talking about exports and arrays, note that arrays generally cannot be exported.

You can even prevent PATH from taking on duplicate entries (refer to this and this):

typeset -U path

PATH pre-populated

The reason your path already has some entries in it is due to your system shell files setting path for you. This is covered in a couple other posts:

edited Feb 4 at 17:26

answered Jun 11, 2015 at 22:11

Micah Elliott

 

one liner, without opening ~/.zshrc file

echo -n 'export PATH=~/bin:$PATH' >> ~/.zshrc

or

echo -n 'export PATH=$HOME/bin:$PATH' >> ~/.zshrc

To see the effect, do source ~/.zshrc in the same tab or open a new tab

 

answered Dec 13, 2017 at 14:26

Siva Praveen

Worked perfecly on OSX with Zsh shell.

Stryker

Feb 2, 2018 at 16:35

Worked like a charm!

Akbarsha

  1. Added path to ~/.zshrc

    sudo vi ~/.zshrc

    add new path

    export PATH="$PATH:[NEW_DIRECTORY]/bin"
    
  2. Update ~/.zshrc

    Save ~/.zshrc

    source ~/.zshrc

  3. Check PATH

    echo $PATHhttps://stackoverflow.com/posts/56963138/edit)

answered Jul 10, 2019 at 3:43

saneryee

This is the Bash way.

CousinCocaine

Nov 7, 2019 at 15:10

Add a comment

 

 

20

 

OPTION 1: Add this line to ~/.zshrc:

export "PATH=$HOME/pear/bin:$PATH"

After that you need to run source ~/.zshrc in order your changes to take affect OR close this window and open a new one

OPTION 2: execute it inside the terminal console to add this path only to the current terminal window session. When you close the window/session, it will be lost.

 

Share

Improve this answer

Follow

edited Sep 13, 2018 at 8:53

answered May 8, 2018 at 12:12

user avatar

Dimitar

1,68355 gold badges2929 silver badges4747 bronze badges

Add a comment

 

 

1

 

for me PATH=$PATH:/path/to/file/bin then export PATH worked. to check echo $PATH . other solutions are adding the path temporarily.

 

Share

Improve this answer

Follow

answered Oct 30, 2021 at 13:26

user avatar

Aurora

9188 bronze badges

Add a comment

 

 

0

 

to verify your new directory has been added correctly, you can use

print -l $path

thanks to the fact that its type is known to be an array

 

Share

Improve this answer

Scroll to Top