【问题标题】:-bash export : not a valid identifier-bash 导出:不是有效的标识符
【发布时间】:2020-11-06 19:08:57
【问题描述】:

我最近在 MacBook 上的 /.bash_profile 中编辑了一些行,以将 firebase 添加到我的学校项目中。

我使用了echo export PATH="$PATH:/Users/quanan/Downloads/flutter/bin",但它没有按我的意愿工作(它确实添加了 ~/.bash_profile)所以我使用sed $d ~/.bash_profile 将其删除并使用cat ~/.bash_profile 检查。然后我看到它没有改变任何东西(或者它改变了但我不知道),我运行了几次 sed 命令并用 cat 检查,终端仍然显示相同的代码。

但是,在这些更改之后,每当我启动终端时,它总是会显示以下两行:

bash: export: `Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/quanan/Downloads/flutter/bin:/Users/quanan/Downloads/flutter/.pub-cache/bin': not a valid identifier

bash: export: `Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/quanan/Downloads/flutter/bin:/Users/quanan/Downloads/flutter/.pub-cache/bin': not a valid identifier

这是我的 ~/.bash_profile

# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave`
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin"
export PATH=/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/quanan/Downloads/flutter/bin:/Users/quanan/Downloads/flutter/.pub-cache/bin
export PATH=/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/quanan/Downloads/flutter/bin:/Users/quanan/Downloads/flutter/.pub-cache/bin
export PATH="$PATH:/Users/quanan/Downloads/flutter/bin"

我检查了这个Link 有类似的错误,他们说要注释错误行但是当我注释 2 行时,终端无法运行一些基本命令,如 cat、mv 或 nano。

所以我对这个错误有一些疑问。

  1. 此错误对我的系统有什么影响? (虽然我的应用仍然运行良好)
  2. 对我的笔记本电脑有害吗?我该如何解决这个错误?
  3. (题外话)为什么一定要和python有关?是不是因为我之前安装的Python IDLE?

【问题讨论】:

  • 由于你的shell命令引用不正确,你遇到了一堆问题;例如,在sed $d ~/.bash_profile 中,$d 将被“扩展”为变量d 的值,该变量可能不存在,因此它实际上从命令中消失了,sed 做了一些不同的事情你自找的。 echo 中也有类似的问题,但它们与您的 .bash_profile 中的损坏不太匹配。要解决此问题,您必须从头开始重建 .bash_profile。 .bash_profile.pysave 中有什么?
  • @Gordon 我试图用谷歌搜索 .bash_profile.pysave 在哪里,我找到了This page,我使用了命令grep -i pysave ~/* ~/.*,它显示的内容如下:/Users/quanan/.bash_profile:#原版保存在.bash_profile.pysave grep: /Users/quanan/.bash_sessions: 是一个目录 grep: /Users/quanan/.config: 是一个目录 还有更多但是太长了,看起来都一样。我的电脑中似乎没有 .bash_profile.pysave 。我能做什么
  • 只需使用cat ~/.bash_profile.pysave(您发现的页面涉及查找使用 .pysave 文件而非文件本身的内容)。
  • 我刚刚做了,终端显示No such file or directory

标签: macos android-studio flutter terminal


【解决方案1】:

我不确定到底出了什么问题,或者您的 .bash_profile 应该如何设置。在这一点上,我建议完全替换它(可能在文本编辑器中——尝试使用 shell 命令可能只会导致更多问题)。这是我对它应该包含什么的最佳猜测:

# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH

PATH="$PATH:/Applications/VMware Fusion.app/Contents/Public
PATH="$PATH:/usr/local/share/dotnet"
PATH="$PATH:~/.dotnet/tools"
PATH="$PATH:/Library/Frameworks/Mono.framework/Versions/Current/Commands"
PATH="$PATH:/Users/quanan/Downloads/flutter/bin"
PATH="$PATH:/Users/quanan/Downloads/flutter/.pub-cache/bin"

PATH 中的一些新增内容可能是多余的/无关的;如果不进一步了解您的系统是如何设置的以及您正在使用什么工具,我无法判断。

至于造成这种情况的原因,似乎是由于引用不正确引起的一些问题。例如,如果你使用了这个(注意:我不认为正是你做了什么):

echo export PATH="$PATH:/Users/quanan/Downloads/flutter/bin" >>~/.bash_profile

...那么外壳程序所做的就是在将字符串传递给echo 之前将$PATH 替换为PATH 变量的当前值(这不是你想要的——你想要它将$PATH 字面意思添加到 .bash_profile,因此仅在该脚本运行时才会扩展)。此外,双引号在传递给echo 之前被删除,所以echo 实际打印的内容类似于:

export PATH=/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/quanan/Downloads/flutter/bin:/Users/quanan/Downloads/flutter/.pub-cache/bin

...并且由于 没有 有双引号,因此“...VMware Fusion.app...”部分中的空格被视为两个单独的分隔符之间的分隔符要导出的东西,第二个(开始“Fusion.app/Contents/Public ...”不是一个有效的变量名,你会得到一个错误。另外,第一部分显然不完整,所以它不会导致一个错误,但它也没有做正确的事情。

要让变量在正确的时间正确扩展,您必须使用类似这样的东西:

echo 'export PATH="$PATH:/Users/quanan/Downloads/flutter/bin"' >>~/.bash_profile

在这里,围绕整个内容的单引号告诉 shell 将 $ 和双引号视为文字字符,因此它们直接通过并包含在添加到 .bash_profile 脚本中的内容中。然后,当它运行时,双引号允许 $PATH 位扩展,但防止“...VMware Fusion.app...”部分被拆分。

这里的寓意是引用至关重要;不仅要这样做,而且要做到正确。如果你不明白引号的作用(以及单引号和双引号之间的区别),你应该小心复制那些做过的人,或者至少检查结果以确保它在做你认为的那样。

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多