在 bash 脚本中找不到 mkdir 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29826442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:57:25  来源:igfitidea点击:

mkdir command not found in bash script

bashmkdir

提问by MLSC

I don't know why I get error while running this simple script:

我不知道为什么在运行这个简单的脚本时会出错:

#!/bin/bash

read -p "Please enter directory name: " DIR
read -p "Please enter the path: " PATH
mkdir -p "$PATH/$DIR"
line 7: mkdir: command not found

回答by Barmar

Don't use the variable PATH. This variable contains a list of directories to search for executable programs. Since you're replacing it, the script can no longer find the mkdirprogram.

不要使用变量PATH。此变量包含用于搜索可执行程序的目录列表。由于您正在替换它,脚本无法再找到该mkdir程序。

In general, avoid using variables that are all uppercase, these are often used as parameters for the shell or other programs.

通常,避免使用全部大写的变量,这些变量通常用作 shell 或其他程序的参数。

回答by cdarke

The variable PATHis an important environment variable - it is the way that programs (like mkdir) are found, and you are overwriting it. You shouldn't do that, but if you must then:

变量PATH是一个重要的环境变量 - 它是程序(如mkdir)的找到方式,您正在覆盖它。你不应该这样做,但如果你必须这样做:

/bin/mkdir -p "$PATH/$DIR"

but honestly DON'T USE UPPERCASE! There are loads of reserved or special variables in Bash, and if you can't remember them all then just remember that all except one is in UPPERCASE. Variables in Bash are case-sensitive, like in all sensible programming languages.

但老实说,不要使用大写!Bash 中有大量的保留变量或特殊变量,如果您不能全部记住它们,那么请记住,除了一个之外的所有变量都是大写的。Bash 中的变量区分大小写,就像在所有合理的编程语言中一样。