为什么在输入命令运行程序时,会自动在程序名前面插入 ./ ?

huangapple go评论85阅读模式
英文:

Why when type command to run a program, auto insert ./ at previous of program name?

问题

例如,

package main

import (
   "fmt"
    "net/http"
)

func hello(res http.ResponseWriter, req *http.Request) {
    fmt.Fprint(res, "你好,我叫Inigo Montoya")
}

func main() {
    http.HandleFunc("/", hello)
    http.ListenAndServe("localhost:4000", nil)
}

当输入命令时,为什么不是inigo,而是自动转换为./inigo

为什么在输入命令运行程序时,会自动在程序名前面插入 ./ ?

英文:

For example,

package main

import (
   "fmt"
    "net/http"
)

func hello(res http.ResponseWriter, req *http.Request) {
    fmt.Fprint(res, "Hello, my name is Inigo Montoya")
}

func main() {
    http.HandleFunc("/", hello)
    http.ListenAndServe("localhost:4000", nil)
}

为什么在输入命令运行程序时,会自动在程序名前面插入 ./ ?

When type command , Why not inigo , auto convert to ./inigo ?

答案1

得分: 2

你需要使用以下命令:

./inigo

因为 . 不在你的 PATH 中,也不应该在其中。 . 只是指当前目录。

假设你正在使用 bash,根据问题的标签,这个替代方法也可以:

$PWD/inigo

同样,任何其他相对或绝对路径到 inigo 的方式也可以正常工作。

编辑

另外,你可以将 $PWD 添加到你的 PATH 中,这样做的好处是不需要输入上述任何命令,而且不会有添加 . 的安全隐患。

假设你在包含 inigo 的目录中,然后执行以下命令:

PATH+=:$PWD

然后你只需要在任何地方输入以下命令:

inigo
英文:

You need

./inigo

because . is not, and it should not be, in your PATH. . just refers to the current directory.

Assuming you are using bash, as the label to the question suggests, this alternative

$PWD/inigo

would also work as well as any other relative or absolute path to inigo.

edit

Alternative, you can add $PWD to your PATH which will have the advantage of not requiring you to type any of the above without having the security implications of adding ..

Let's say you are in the directory that contains inigo, then

PATH+=:$PWD

and then just type

inigo

from anywhere.

huangapple
  • 本文由 发表于 2023年2月23日 14:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75541362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定