简体   繁体   English

如何在$ PATH变量中添加目录,以便可以在bash中运行脚本而无需输入整个文件路径?

[英]How can I add a directory to my $PATH variable so that I may run scripts in bash without typing out the entire file path?

I want to run a bash script "hello.sh" from the command line using only the file name and extension. 我想仅使用文件名和扩展名从命令行运行bash脚本“ hello.sh”。 I added the directory where this file exists ("/Users/myusername/dev"), into the $PATH variable by editing the .bash_profile file. 通过编辑.bash_profile文件,我将该文件所在的目录(“ / Users / myusername / dev”)添加到$ PATH变量中。 However, when I try to run the script, I get "command not found". 但是,当我尝试运行脚本时,出现“找不到命令”。 I am working on MacOSX. 我正在MacOSX上工作。

Here is my $PATH variable, verified from the Terminal app: “/Users/myusername/dev:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin” 这是我的$ PATH变量,已通过终端应用验证:“ / Users / myusername / dev:/Library/Frameworks/Python.framework/Versions/3.4/bin:/ usr / local / bin:/ usr / bin:/ bin :/ usr / sbin:/ sbin”

If I move the script into the /usr/local/bin folder, also in my $PATH, the script can be run by typing it directly. 如果将脚本移到$ usr的/ usr / local / bin文件夹中,也可以直接键入脚本来运行该脚本。 One thought I have is that there might be some user rights I need to set for the directory? 我有一种想法是可能需要为目录设置一些用户权限? Otherwise, did I not set the path correctly within $PATH? 否则,是否没有在$ PATH中正确设置路径?

Two possibilities: 两种可能性:

  • permissions seem less likely to be a problem, but you do have to have both read- and execute-permission on the /Users/myusername/dev directory (and its parent, etc.). 权限似乎不太可能是一个问题,但必须同时具有只读和执行许可权上的/用户/名为myUsername / dev目录(及其父母等)。
  • .bash_profile is read for login-shells. 读取.bash_profile作为登录外壳。 You may have overlooked either sourcing the file, eg, . ~/.bash_profile 您可能忽略了源文件,例如. ~/.bash_profile . ~/.bash_profile or logging out/in. . ~/.bash_profile或注销/登录。

To check typos, I use a script that shows my path in readable form (see link ). 为了检查错别字,我使用了一个脚本,该脚本以可读的形式显示了我的路径(请参阅链接 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在bash中的文件路径中使用变量吗? 如果是这样,怎么样? - Can I use a variable in a file path in bash? If so, how? 当路径可能为空或可能不为空时,如何将路径永久添加到与路径相关的环境变量中? - How can I permanently add a path to a path-related environment variable, when it may or may not be empty? 如何从 bash 的 PATH 环境变量中获取第一个目录? - How to I get the first directory from the PATH environment variable in bash? 如何在我的机器上没有重复的自定义 bash 脚本目录并将其添加为使用 PATH 获取的链接? - How do I not have a duplicated custom bash script directory on my machine and add it as a link that is picked up with PATH? 如何在bash上添加/设置Ruby路径? - How can I add/set Ruby path on bash? 如何/在哪里可以在我的计算机上保存Bash脚本,以便我的终端可以执行更多命令? - How/where can I save Bash scripts on my computer so that my terminal can execute more commands? 如何从任何目录执行Bash脚本来源其他Bash脚本(不使用路径变量) - How to execute from any directory Bash script that sources other Bash scripts (not using path variable) 如何在UNIX中将当前目录添加到搜索路径? - How can I add the current directory to the search path in UNIX? Bash路径变量使用〜导致“没有此类文件或目录” - Bash path variable using ~ resulting in 'No such file or directory' 如何在不更改脚本的情况下在 bash 脚本中使用别名? - How can I use aliases in bash scripts without changing the scripts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM