Linux中的source命令

罗德诺尔

我的问题是:
为什么要运行带有名称别名的文件,例如具有以下内容的文件:

alias lsa="ls -a" 

直接地:

$ ./aliases

它不会创建别名(可能仅在脚本上下文中)。
但是,如果我使用命令“ source”运行它:

$ source aliases

它做的工作吗?我的意思是执行后在命令外壳上下文中存在别名“ lsa”吗?
“ man源代码”给出:“没有手动输入源代码”,在google中,我刚刚发现它运行Tcl,但是为什么Tcl影响外壳上下文而不影响套管?

m4573r

基本上是因为当您运行时./aliases,它将创建一个进程,该进程中存在您的别名,但别名会在此之后立即终止,而当您运行source时,它将应用于您当前的bash进程。

要获得帮助source,您需要阅读man bash为您省去麻烦:

source filename [arguments]
    Read and execute commands from filename in the current shell environment
    and return the exit status of the last command executed from filename.
    If filename does not contain a slash, file names in PATH are used to find
    the directory containing filename. The file searched for in PATH need not
    be executable. When bash is not in posix mode, the current directory is
    searched if no file is found in PATH. If the sourcepath option to the shopt
    builtin command is turned off, the PATH is not searched. If any arguments
    are supplied, they become the positional parameters when filename is
    executed. Otherwise the positional parameters are unchanged. The return
    status is the status of the last command exited within the script (0 if
    no commands are executed), and false if filename is not found or cannot
    be read.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章