如何在终端中的可执行文件名称之前解释“./"前缀? [英] How to explain './' prefix before the name of an executable in terminal?

查看:38
本文介绍了如何在终端中的可执行文件名称之前解释“./"前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我编译 C 程序时,比如使用 cc 编译器,我都会在当前目录中得到一个可执行文件.现在如果我想运行它,而不是只输入 a.outname_of_executable,我必须用这个组合前缀 ./a.out.

Every time I compile a C program, say with the cc compiler, I get an executable in the current directory. Now if I want to run it, instead of typing out just a.out or name_of_executable, I have to prefix that with this combination ./a.out.

我理解.(链接到当前目录)和..(链接到其父目录)背后的含义,还有/ 是目录名之间的分隔符.

I understand the meaning behind . (link to the current directory) and .. (link to its parent directory), also that / is a separator between directory names.

但是./是什么意思?它只是一种正式的方式来区分引用某事物的名称和运行该事物的意图(因为您的文件名中不能包含 /)?

But what is the meaning of ./? Is it just a formal way to separate between referring to a name of something, and the intention to run that something (because you can't have / in your filename)?

推荐答案

当您键入可执行程序的名称时,shell 会在名称存储在 $PATH环境变量.

When you type the name of an executable program, the shell searches for it in a sequence of directories whose names are stored in the $PATH environment variable.

当前目录 . 通常不在(也不应该)在您的 $PATH 中.因此,如果您只键入 a.out,shell 将找不到它——除非碰巧有 /usr/bin/a.out/usr/local/bin/a.out 或 ....

The current directory, ., normally isn't (and shouldn't be) in your $PATH. So if you type just a.out, the shell won't find it -- unless there happens to be a /usr/bin/a.out, or /usr/local/bin/a.out, or ....

为什么不应该将 . 放在您的 $PATH 中?(有时,在某些系统上,确实如此.)因为它会造成安全漏洞.如果 . 在您的 $PATH 的前面,那么如果您 cd 到一个目录,该目录恰好包含一个名为 ls 的命令,很容易被不小心执行,造成任意不良后果.即使 . 位于 $PATH 的末尾,如果您刚刚编译的小程序或您刚刚编写的脚本碰巧有与标准命令同名.我见过很多人因为命名一个测试程序 test 而变得非常困惑,但是输入 test 运行 /bin/test.

Why shouldn't . be in your $PATH? (Sometimes, on some systems, it is.) Because it creates a security hole. If . is at the front of your $PATH, then if you cd to a directory that happens to contain a command called ls, it's very easy to execute it accidentally, with arbitrarily bad consequences. Even if . is at the end of $PATH, you can still run into problems if the little program you just compiled, or the script you just wrote, happens to have the same name as a standard command. I've seen a lot of people becoming very confused because the named a test program test, but typing test runs /bin/test.

命令 可以是内置命令(由 shell 提供)或可执行文件的名称.如果您键入 ls,shell 会在您的 $PATH 中指定的目录之一中找到一个名为 ls 的可执行文件.如果您键入 a.out,则在 $PATH 中命名的任何目录中都没有该名称的可执行文件——因此是错误消息.

A command can be either a built-in command (provided by the shell) or the name of an executable file. If you type ls, the shell finds an executable file called ls in one of the directories named in your $PATH. If you type a.out, there is no executable file of that name in any of the directories named in $PATH -- thus the error message.

输入可执行文件的路径(即使是像./a.out这样的相对路径)会导致shell绕过$PATH 搜索;您是在告诉 shell 在哪里可以找到可执行文件,而不是让 shell 搜索它.

Typing a path to the executable file (even a relative path like ./a.out) causes the shell to bypass the $PATH search; you're telling the shell exactly where to find the executable file rather than asking the shell to search for it.

这篇关于如何在终端中的可执行文件名称之前解释“./"前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆