What does -n mean in Bash

倖福魔咒の 提交于 2020-03-14 07:31:14

问题


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 -n is 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.


回答1:


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

You can learn more with help test.

$1 and 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 foo as ./foo bar baz would result in $1 == bar, $2 == baz




回答2:


-n is one of the string operators for evaluating the expressions in bash. It tests the string next to it and evaluates it as "True" if string is non empty.

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

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



来源:https://stackoverflow.com/questions/10475599/what-does-n-mean-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!