bash 如何在 OSX 上编辑 $PATH (.bash_profile)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30461201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:59:04  来源:igfitidea点击:

How do I edit $PATH (.bash_profile) on OSX?

bashmacosterminalpathosx-yosemite

提问by Joolah

I am trying to edit an entry to PATH, as I did something wrong.

我正在尝试编辑 PATH 的条目,因为我做错了什么。

I am using Mac OS X 10.10.3

我使用的是 Mac OS X 10.10.3

I have tried:

我试过了:

> touch ~/.bash_profile; open ~/.bash_profile

But the file editor opens with nothing inside.

但是文件编辑器打开时里面什么也没有。

My problem:

我的问题:

I am trying to install ANDROID_HOME to my PATH

我正在尝试将 ANDROID_HOME 安装到我的 PATH

I misspelled it, but when I closed the terminal and went back it was gone, so I tried again:

我拼错了,但是当我关闭终端并返回时它不见了,所以我再次尝试:

export ANDROID_HOME=/<installation location>/android-sdk-macosx
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
export ANDROID_HOME=/<installation location>/android-sdk-macosx
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

This time, I typed the command correctly but, when I closed the terminal, my settings disappeared again.

这一次,我正确输入了命令,但是当我关闭终端时,我的设置又消失了。

How do I execute my desired settings?

如何执行我想要的设置?

If I was to edit bash.profile, how would I enter the above code?

如果我要编辑bash.profile,我该如何输入上面的代码?

Thanks!

谢谢!

回答by Alessandro Vendruscolo

You have to open that file with a text editor and then save it.

您必须使用文本编辑器打开该文件,然后保存它。

touch ~/.bash_profile; open ~/.bash_profile

It will open the file with TextEdit, paste your things and then save it. If you open it again you'll find your edits.

它将使用 TextEdit 打开文件,粘贴您的内容,然后保存。如果你再次打开它,你会发现你的编辑。

You can use other editors:

您可以使用其他编辑器:

nano ~/.bash_profile
mate ~/.bash_profile
vim ~/.bash_profile

But if you don't know how to use them, it's easier to use the openapproach.

但是,如果您不知道如何使用它们,则使用该open方法会更容易。



Alternatively, you can rely on pbpaste. Copy

或者,您可以依赖pbpaste. 复制

export ANDROID_HOME=/<installation location>/android-sdk-macosx
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

in the system clipboard and then in a shell run

在系统剪贴板中,然后在 shell 中运行

pbpaste > ~/.bash_profile


Or alternatively you can also use cat

或者你也可以使用 cat

cat > ~/.bash_profile

(now catwaits for input: paste the two export definitions and then hit ctrl-D).

(现在cat等待输入:粘贴两个导出定义,然后按 ctrl-D)。

回答by Herr_Hansen

A bit more detailed for beginners:

对于初学者来说更详细一点:

First, make sure the .bash_profile file is existing?Remember that the .bash_profile file isn't there by default. You have to create it on your own.

首先,确保.bash_profile 文件存在?请记住,默认情况下 .bash_profile 文件不存在。您必须自己创建它。

Go into your user folder in finder. The .bash_profile file should be findable there. -> HD/Users/[USERNAME]

进入finder 中用户文件夹。.bash_profile 文件应该可以在那里找到。-> HD/用户/[用户名]

Remember: Files with a point at the beginning '.' are hiddenby default.

请记住:以“.”开头文件 默认情况下是隐藏的。

To show hidden files in Mac OS Finder:

在 Mac OS Finder 中显示隐藏文件:

Press: Command + Shift + .

If it's not existing, you have to create .bash_profileon your own.

如果它不存在,您必须自己创建 .bash_profile

Open terminalapp and switch into user folderwith simple command:

打开终端应用程序并使用简单的命令切换到用户文件夹

cd

If it's not existing, use this command to create the file:

如果它不存在,请使用此命令创建文件

touch .bash_profile

Second, if you can't memorise the nerdy commands for save and close in vim, nano etc (the way recommended above) the easiest way to edit is to open .bash_profile file in your favored code editor (Sublime etc.).

其次,如果您无法记住 vim、nano 等(上面推荐的方式)中保存和关闭的书呆子命令,最简单的编辑方法是在您喜欢的代码编辑器(Sublime 等)中打开 .bash_profile 文件。

Finder -> User folder. Right click -> open with : Sublime Text (or other code editor). Or drag it on app in dock.

Finder -> 用户文件夹。右键单击 -> 打开方式:Sublime Text(或其他代码编辑器)。或将其拖到 Dock 中的应用程序上。

… and there you can edit it, pass export commands in new lines.

...在那里你可以编辑它,在新行中传递导出命令。

回答by Marcus Gallegos

If you are using MAC Catalina you need to update the .zshrc file instead of .bash_profile or .profile

如果您使用的是 MAC Catalina,则需要更新 .zshrc 文件而不是 .bash_profile 或 .profile

回答by Myles O'Connor

For beginners: To create your .bash_profilefile in your home directory on MacOS, run:

对于初学者:要.bash_profile在 MacOS 上的主目录中创建文件,请运行:

nano ~/.bash_profile

nano ~/.bash_profile

Then you can paste in the following:

然后您可以粘贴以下内容:

https://gist.github.com/mocon/0baf15e62163a07cb957888559d1b054

https://gist.github.com/mocon/0baf15e62163a07cb957888559d1b054

As you can see, it includes some example aliases and an environment variable at the bottom.

如您所见,它在底部包含一些示例别名和一个环境变量。

One you're done making your changes, follow the instructions at the bottom of the Nano editor window to WriteOut (Ctrl-O) and Exit (Ctrl-X). Then quit your Terminal and reopen it, and you will be able to use your newly defined aliases and environment variables.

完成更改后,请按照 Nano 编辑器窗口底部的说明进行 WriteOut ( Ctrl-O) 和 Exit ( Ctrl-X)。然后退出您的终端并重新打开它,您将能够使用新定义的别名和环境变量。

回答by canerkaseler

In Macbook, step by step:

在 Macbook 中,一步一步:

  1. First of all open terminal and write it: cd ~/
  2. Create your bash file: touch .bash_profile
  1. 首先打开终端并编写它: cd ~/
  2. 创建你的 bash 文件: touch .bash_profile

You created your ".bash_profile" file but if you would like to edit it, you should write it;

你创建了你的“.bash_profile”文件,但如果你想编辑它,你应该写它;

  1. Edit your bash profile: open -e .bash_profile
  1. 编辑您的 bash 配置文件: open -e .bash_profile

After you can save from top-left corner of screen: File > Save

从屏幕左上角保存后文件>保存

@canerkaseler

@canerkaseler

回答by neemo810707

Mac OS X doesn't store the path in .bash_profile, but .profile, since Mac OS X is a branch of *BSD family. You should be able to see the export blah blah blah in .profile once you do cat .profile on your terminal.

Mac OS X 不在 .bash_profile 中存储路径,而是在 .profile 中,因为 Mac OS X 是 *BSD 系列的一个分支。一旦您在终端上执行 cat .profile ,您应该能够在 .profile 中看到 export blah blah blah 。

回答by Bheem Singh

Set the path JAVA_HOME and ANDROID_HOME > You have to open terminal and enter the below cmd.

设置路径 JAVA_HOME 和 ANDROID_HOME > 您必须打开终端并输入以下 cmd。

touch ~/.bash_profile; open ~/.bash_profile

After that paste below paths in base profile file and save it

之后粘贴到基本配置文件中的路径下面并保存

export ANDROID_HOME=/Users/<username>/Library/Android/sdk 
export PATH="$JAVA_HOME/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH"
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

回答by ALI HUSSEIN

For me my mac OS is Mojave. and I'm facing the same issue for three days and in the end, I just write the correct path in the .bash_profile file which is like this:

对我来说,我的 Mac 操作系统是 Mojave。我三天都面临同样的问题,最后,我只是在 .bash_profile 文件中写了正确的路径,如下所示:

    export PATH=/Users/[YOURNAME]/development/flutter/bin:$PATH
  • note1: if u don't have .bash_profile create one and write the line above
  • note2: zip your downloaded flutter SDK in [home]/development if you copy and paste this path
  • 注意1:如果你没有 .bash_profile 创建一个并写上一行
  • 注意2:如果您复制并粘贴此路径,请将您下载的 flutter SDK 压缩到 [home]/development