source .bashrc in a script not working

前端 未结 1 1622
走了就别回头了
走了就别回头了 2020-12-16 05:57

I am doing a script that is installing ros and after installing it, compiling a workspace with catkin_make.

I found the solution to solve my problem but I can\'t exp

相关标签:
1条回答
  • 2020-12-16 06:36

    Some platforms come with a ~/.bashrc that has a conditional at the top that explicitly stops processing if the shell is found to be non-interactive.

    For example, on Ubuntu 18.04:

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    A similar test, seen in /etc/bash.bashrc on the same platform:

    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return
    

    If this is the case, sourcing ~/.bashrc from a script will have no effect, because scripts run in non-interactive shells by default.

    Your options are:

    • Either: deactivate the conditional in ~/.bashrc

    • Or: Try to to emulate an interactive shell before invoking source ~/.bashrc.
      The specific emulation needed depends on the specifics of the conditional, but there are two likely approaches; you may have to employ them both if you don't know ahead of time which conditional you'll encounter:

      • set -i temporarily to make $- contain i, indicating an interactive shell.
      • If you know the contents of the line that performs the interactivity test, filter it out of the ~/.bashrc using grep, and then source the result with eval (the latter should generally be avoided, but it in this case effectively provides the same functionality as sourcing).
        Note that making sure that environment variable PS1 has a value is not enough, because Bash actively resets it in non-interactive shells - see this answer for background information.
        • eval "$(grep -vFx '[ -z "$PS1" ] && return' ~/.bashrc)"

    Alternatively, if you control how your own script is invoked, you can invoke it with
    bash -i script.

    0 讨论(0)
提交回复
热议问题