假设我在True
中创建并编译了一个小程序来计算文件的字节数,称为C
。要运行它,我会使用filebyte
现在我想在bash上使它成为通用的,比如运行一个php文件,我会使用./filebyte
命令bash
,就像我想运行我的程序一样,{{1 }}
我该怎么做?
谢谢!
答案 0 :(得分:3)
我经常在我的主目录中创建一个bin/
目录,用于小型自定义应用程序。
然后,您需要将该目录添加到PATH,这是一个冒号分隔的路径列表,当您在thr命令行上键入名称时,shell会搜索可执行文件。
这通常是通过将其放入~/.bashrc
文件中来完成的:
PATH="$PATH:~/bin"
答案 1 :(得分:1)
检查环境变量PATH
并将可执行文件放在列出的其中一个目录中。您也可以将其放在自定义目录中,然后将其附加到PATH
。您可以执行printenv PATH
答案 2 :(得分:1)
如果您想单独使用当前的活动shell,请执行
export PATH=$PATH:</path/to/file>
要永久使文件可用,请将以上行添加到~/.bashrc
为什么要将其添加到PATH
变量中,man bash
说明原因,
PATH The search path for commands. It is a colon-separated list of
directories in which the shell looks for commands (see COMMAND
EXECUTION below). A zero-length (null) directory name in the
value of PATH indicates the current directory. A null directory
name may appear as two adjacent colons, or as an initial or
trailing colon. The default path is system-dependent, and is set
by the administrator who installs bash. A common value is
''/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''.