【问题标题】:Prioritize file in ~/.local/bin over /usr/bin~/.local/bin 中的文件优先于 /usr/bin
【发布时间】:2022-01-31 04:47:28
【问题描述】:

我正在尝试用~/.local/bin/ 中的脚本替换whoami 命令。有没有办法让我的 whoami 获得优先权,这样当我运行 whoami 时,我的脚本就会运行?

【问题讨论】:

    标签: linux bash shell ubuntu bin


    【解决方案1】:

    这是我在~/.profile 文件中的内容

    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/.local/bin" ] ; then
        PATH="$HOME/.local/bin:$PATH"
    fi
    

    这行得通!

    在这个文件的顶部,写着

    # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
    # exists.
    

    所以如果您有 ~/.bash_profile~/.bash_login 删除它们或将它们与~/profile 合并。我没有这些文件。

    这就是我的$PATH 的样子(缩短了……)

    $ echo $PATH | sed -r 's/:/\n/g'| cat -n
         1  /home/shiplu/anaconda3/condabin
         2  /home/shiplu/.sdkman/candidates/gradle/current/bin
         3  /home/shiplu/.cargo/bin
         4  /home/shiplu/.local/bin
         5  /home/shiplu/bin
         6  /usr/local/sbin
         7  /usr/local/bin
         8  /usr/sbin
         9  /usr/bin
        10  /sbin
        11  /bin
        12  /usr/games
        13  /usr/local/games
        14  /snap/bin
    

    检查第 4 行的路径。它位于任何其他系统 bin 目录之前。

    【讨论】:

    • 好的,所以我已经知道包含私人垃圾箱了,我没有~/.bash_profile,也没有~/.bash_login,它仍然无法正常工作。还有其他东西应该是采购.profile 但不是吗?
    • .profile 会在您登录时添加到您的环境中。 Iit 用于登录外壳。您也可以在~/.bashrc 中添加这些行。每次打开新的 bash 实例时都会包含此内容。因此,首先尝试重新启动/(重新登录)您的 PC。如果它仍然不起作用,则将这些行添加到 .bashrc
    • 谢谢,将行添加到.bashrc 似乎有效。
    【解决方案2】:

    您需要将~/.local/bin/ 首先放在您的PATH 环境变量中。然后将首先检查该目录,然后再检查其他程序是否运行。

    export PATH=~/.local/bin:$PATH
    

    【讨论】:

    • 好的,我尝试了您的建议,但是我必须手动获取 ~/.profile,在此之前,我的 PATH 没有 ~/.local/bin/,但是在我手动获取它之后,一切正常。我尝试使用crontab -e 将其添加到我的 crontab 中,但没有这样的运气。我输入了@reboot source /home/(my username)/.profile。我做错了吗?
    • @sketch54 cron 作业在后台运行,与您的交互式 shell 没有任何连接。您需要找出您正在使用的 shell(bash、zsh 等)以及它在启动时运行的初始化文件(可能是 ~/.bash_profile 或 ~/.bash_login 或 ~/.zshrc 或 ~/.zshenv或...)并将该命令放入适当的文件中。
    猜你喜欢
    • 2023-03-05
    • 2013-04-22
    • 2021-03-25
    • 1970-01-01
    • 2014-08-05
    • 2014-12-17
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多