-n 在 Bash 中是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10475599/
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-18 02:12:28  来源:igfitidea点击:

What does -n mean in Bash

bashparameters

提问by UNECS

This is probably a really dumb question, but... if [ ! -n "$1" ], means, if not more than 1 argument do... so I get how it works but what is -nis it the abbreviation of number? I've been reading through The Advanced bash programming guide and they just start using it, I've tried to find it and come up with it must be a "built-in" default parameter, second question is there a command to show default parameter's in Linux.

这可能是一个非常愚蠢的问题,但是...如果 [ ! -n "$1" ], 意思是,如果不超过 1 个参数做...所以我知道它是如何工作的,但-n它的缩写是什么数字?我一直在阅读高级 bash 编程指南,他们刚刚开始使用它,我试图找到它并提出它必须是一个“内置”默认参数,第二个问题是有一个命令来显示默认值Linux 中的参数。

回答by Daenyth

The -nargument to test(aka [) means "is not empty". The example you posted means "if $1is not not empty. It's a roundabout way of saying [ -z "$1" ];($1is empty).

(aka )的-n参数表示“不为空”。您发布的示例的意思是“如果不是空的。这是一种迂回的说法(是空的)。test[$1[ -z "$1" ];$1

You can learn more with help test.

您可以通过 了解更多信息help test

$1and others ($2, $3..) are positional parameters. They're what was passed as arguments to the script or function you're in. For example, running a script named fooas ./foo bar bazwould result in $1 == bar, $2 == baz

$1和其他 ( $2, $3..) 是位置参数。它们是作为参数传递给您所在的脚本或函数的内容。例如,运行名为fooas的脚本./foo bar baz将导致$1 == bar,$2 == baz

回答by Tejeshreddy

-nis one of the string operatorsfor evaluating the expressions in bash. It tests the stringnext to it and evaluates it as "True" if stringis non empty.

-n是用于评估 bash 中表达式的字符串运算符之一。它测试旁边的字符串,如果字符串不为空,则将其评估为“真” 。

Positional parametersare a series of special variables ($0, $1through $9) that contain the contents of the command line argument to the program. $0contains the name of the program and other contains arguments that we pass.

位置参数是一系列特殊变量($0,$1$9),其中包含程序的命令行参数的内容。 $0包含程序的名称,其他包含我们传递的参数。

Here, [ ! -n "$1" ]evaluates to "True" if second positionalparameter ($1) passed to the program is emptyor (in other words) if no arguments are passed to the program other than $0.

此处,[ ! -n "$1" ]如果传递给程序的第二个位置参数 ( $1) 为空,或者(换句话说)如果除 之外没有其他参数传递给程序,则计算结果为“真” $0