Replacement for source in sh

后端 未结 2 1082
无人共我
无人共我 2020-12-04 21:12

I need to set the environment variables, usually we do this by

source script.sh

But now, I am automating it during the boot process and it

2条回答
  •  北海茫月
    2020-12-04 21:20

    tl;dr With sh (as opposed to bash) the argument must contain a slash: source ./script.sh, not just source script.sh. Unless script.sh can be found in PATH.

    Dot (.) command exists in both bash and sh. Additionally, bash aliases it as source. From bash manual:

    https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-source

    source

    source filename
    

    A synonym for . (see Bourne Shell Builtins).

    https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-_002e

    . (a period)

    . filename [arguments]
    

    Read and execute commands from the filename argument in the current shell context. If filename does not contain a slash, the PATH variable is used to find filename. When Bash is not in POSIX mode, the current directory is searched if filename is not found in $PATH.

    From POSIX:

    If file does not contain a /, the shell shall use the search path specified by PATH to find the directory containing file. Unlike normal command search, however, the file searched for by the dot utility need not be executable.

提交回复
热议问题