这两者之间有什么区别?文件”和“。/文件“在Linux bash shell中?

时间:2022-03-10 03:43:32

When 'file' is executable, what is the difference between

当'file'是可执行文件时,它们之间的区别是什么

$ ./file

and

$ . file

in Linux bash shell?

在Linux中bash shell ?

1 个解决方案

#1


3  

. is alias for source: it will execute the file that is its argument in the current bash process, just as if you typed the file out in the command line directly.

。是source的别名:它将在当前bash进程中执行作为其参数的文件,就像您直接在命令行中输入文件一样。

./file is just giving a path to a file; in that case, the file is launched as a subprocess, according to the file's type, or its hashbang in case of a text file. If the first line of the file is #!/bin/bash, it will launch it as a new bash process.

./file只是一个文件的路径;在这种情况下,根据文件的类型或文本文件的hashbang,文件作为子进程启动。如果文件的第一行是#!/bin/bash将它作为新的bash进程启动。

./ here just says the file is in the current directory. Saying cd /bin ; ./ls is no different than saying /bin/ls (except obviously for the fact that we change the current directory in one and not the other). The reason why we have to specify the current directory and can't simply give the file's name is that if there is no directory specified, bash only looks in PATH for programs to execute, and current directory is not normally in PATH in Unix-like systems. Since /bin is in PATH, a simple ls is (normally) equivalent to /bin/ls.

./这里只是说文件在当前目录中。说cd /箱;./ls与/bin/ls没有什么不同(除了显然我们在一个目录中更改了当前目录,而没有更改另一个目录)。我们必须指定当前目录而不能简单地给出文件名称的原因是,如果没有指定目录,bash仅在要执行的程序的路径中查找,并且在类unix系统中,当前目录通常不在路径中。由于/bin在路径中,一个简单的ls(通常)等价于/bin/ls

The difference is pretty stark if you want to define variables. Any environment variables only live for the current process and its subprocesses; so if you define an environment variable in a subprocess, the calling process will not have access to it.

如果你想定义变量的话,差别是非常明显的。任何环境变量仅为当前进程及其子进程而存在;因此,如果在子进程中定义环境变量,调用进程将无法访问它。

For example, let's say you have a file called testvar.sh:

例如,假设您有一个名为testvar.sh的文件:

#!/bin/bash

what=world

If you do this, you will not have the variable set:

如果你这样做,你将没有变量集:

./testvar.sh ; echo Hello, $what
# => Hello, 

But if you do this, you will:

但如果你这样做,你会:

. testvar.sh ; echo Hello, $what
# => Hello, world

Another obvious difference is, since . executes bash commands, that it only works on files containing bash scripts. Thus, cd /bin ; ./ls will execute the ls program; but cd /bin ; . ls will fail with an error.

另一个明显的区别是。执行bash命令,它只在包含bash脚本的文件上工作。因此,cd / bin;./ls将执行ls程序;但cd /箱;。ls会因错误而失败。

#1


3  

. is alias for source: it will execute the file that is its argument in the current bash process, just as if you typed the file out in the command line directly.

。是source的别名:它将在当前bash进程中执行作为其参数的文件,就像您直接在命令行中输入文件一样。

./file is just giving a path to a file; in that case, the file is launched as a subprocess, according to the file's type, or its hashbang in case of a text file. If the first line of the file is #!/bin/bash, it will launch it as a new bash process.

./file只是一个文件的路径;在这种情况下,根据文件的类型或文本文件的hashbang,文件作为子进程启动。如果文件的第一行是#!/bin/bash将它作为新的bash进程启动。

./ here just says the file is in the current directory. Saying cd /bin ; ./ls is no different than saying /bin/ls (except obviously for the fact that we change the current directory in one and not the other). The reason why we have to specify the current directory and can't simply give the file's name is that if there is no directory specified, bash only looks in PATH for programs to execute, and current directory is not normally in PATH in Unix-like systems. Since /bin is in PATH, a simple ls is (normally) equivalent to /bin/ls.

./这里只是说文件在当前目录中。说cd /箱;./ls与/bin/ls没有什么不同(除了显然我们在一个目录中更改了当前目录,而没有更改另一个目录)。我们必须指定当前目录而不能简单地给出文件名称的原因是,如果没有指定目录,bash仅在要执行的程序的路径中查找,并且在类unix系统中,当前目录通常不在路径中。由于/bin在路径中,一个简单的ls(通常)等价于/bin/ls

The difference is pretty stark if you want to define variables. Any environment variables only live for the current process and its subprocesses; so if you define an environment variable in a subprocess, the calling process will not have access to it.

如果你想定义变量的话,差别是非常明显的。任何环境变量仅为当前进程及其子进程而存在;因此,如果在子进程中定义环境变量,调用进程将无法访问它。

For example, let's say you have a file called testvar.sh:

例如,假设您有一个名为testvar.sh的文件:

#!/bin/bash

what=world

If you do this, you will not have the variable set:

如果你这样做,你将没有变量集:

./testvar.sh ; echo Hello, $what
# => Hello, 

But if you do this, you will:

但如果你这样做,你会:

. testvar.sh ; echo Hello, $what
# => Hello, world

Another obvious difference is, since . executes bash commands, that it only works on files containing bash scripts. Thus, cd /bin ; ./ls will execute the ls program; but cd /bin ; . ls will fail with an error.

另一个明显的区别是。执行bash命令,它只在包含bash脚本的文件上工作。因此,cd / bin;./ls将执行ls程序;但cd /箱;。ls会因错误而失败。