bash 中使用的 If [[-n variable ]] 语法是什么

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

what is If [[-n variable ]] syntax used for in bash

bashshell

提问by dpsdce

I am fixing some old bash scripts I often see

我正在修复一些我经常看到的旧 bash 脚本

if [[ -n $VARIABLE ]]; then 

syntax I tried to google it but could find why "-n" is used for, following is what I know

语法我试图谷歌它但可以找到为什么使用“-n”,以下是我所知道的

Comparisons:
  -eq   equal to
  -ne   not equal to
  -lt   less than
  -le   less than or equal to
  -gt   greater than
  -ge   greater than or equal to

File Operations:

文件操作:

  -s    file exists and is not empty
  -f    file exists and is not a directory
  -d    directory exists
  -x    file is executable
  -w    file is writable
  -r    file is readable

would anyone let me know what -n do ?

有没有人让我知道 -n 做什么?

回答by devnull

help testwould tell you:

help test会告诉你:

String operators:

  ....

  -n STRING
     STRING      True if string is not empty.

回答by Radu R?deanu

If $VARIABLEis a string, then [ -n $VARIABLE ]is true if the length of $VARIABLEis non-zero.

如果$VARIABLE是字符串,则[ -n $VARIABLE ]如果 的长度$VARIABLE不为零,则为真。

Also, [ -n $VARIABLE ]is equivalent with: [ $VARIABLE ], when and only when $VARIABLEis a string.

此外,[ -n $VARIABLE ]等价于: [ $VARIABLE ], when and only when$VARIABLE是一个字符串。

More about: Introduction to if

更多关于:if 简介

回答by David W.

The various teststhat [[ ... ]]and [ ... ]use in ifand whileloops are from the Unix testcommand itself. An easy way to see what these various tests are is to look the testmanpage.

各种测试[[ ... ]][ ... ]使用ifwhile循环是从Unix的test命令本身。查看这些不同测试的一种简单方法是查看测试联机帮助页。

In Unix, the /bin/[command is actually a hard link to the /bin/testcommand. In early Unix systems, you would write this:

在 Unix 中,/bin/[命令实际上是命令的硬链接/bin/test。在早期的 Unix 系统中,你会这样写:

if test -n $parameter
then
    echo "Parameter has a value"
fi

or

或者

if test $foo = $bar
then
    echo "Foo and Bar are equal"
fi

The /bin/[was created, so you could do this:

/bin/[被创造了,所以你可以这样做:

if [ -n $parameter ]
then
    echo "Parameter has a value"
fi

and this

和这个

if [ $foo = $bar ]
then
    echo "Foo and Bar are equal"
fi

This explains why the funny syntax and why you need a space between the square brackets and the parameters inside.

这解释了为什么有趣的语法以及为什么在方括号和里面的参数之间需要一个空格。

The [[ ... ]]is actually a Korn shellism... I mean a POSIX shellism that BASH has takenborrowed. It was introduced to allow pattern matching tests ([[ $foo == bar* ]]) and is internal to the shell, so its less sensitive to shell command line expansion issues. For example:

[[ ... ]]实际上是一个光辉shellism......我的意思是POSIX shellism的bash采取借用。它被引入以允许模式匹配测试 ( [[ $foo == bar* ]]) 并且是 shell 内部的,因此它对 shell 命令行扩展问题不太敏感。例如:

if [ $foo = $bar ]

will fail if either $fooor $baris not set while:

如果$foo$bar未设置,则将失败:

if [[ $foo = $bar ]]

will work even if one of those two variables aren't set.

即使未设置这两个变量之一也将起作用。

The [[ ... ]]syntax takes all of the same testing parameters that [ ... ]does and is now preferred.

[[ ... ]]语法通吃相同的测试参数的[ ... ]不和,现在首选。