最近电脑装了好多个PHP的版本,如果执行用例如:
/usr/local/opt/php@8.3/bin/php $(which composer)
那样太麻烦了,可以通过配置sell别名,给命令设置一个别名
步骤1:编辑shell配置文件
- 确认Mac是zsh还是bash:
echo $SHELL - 如果是zsh:编辑~/.zshrc ;如果是bash:编辑~/.bash_profile
- 以bash为例,执行打开配置文件命令:
open -e ~/.bash_profile
步骤2:添加别名配置
在打开的配置文件末尾添加一行,并保存:
alias composer83='/usr/local/opt/php@8.3/bin/php $(which composer)'
解释:alias 短命令='原命令' ,这里会将你配置的composer83 代替原来冗长的命令
步骤3:生效配置和测试(无需重启终端)
生效配置:source ~/.bash_profile
测试是否生效:composer83 -V
输出:
Composer version 2.9.2 2025-11-19 21:57:25PHP version 8.3.27 (/usr/local/Cellar/php@8.3/8.3.27_2/bin/php)Run the "diagnose" command to get more detailed diagnostics output.
大功告成。