Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -n mean in Bash?

This is probably a really dumb question, but... if [ ! -n "$1" ], means, if not more than one 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. Is there a command to show default parameters in Linux?

like image 807
UNECS Avatar asked May 07 '12 01:05

UNECS


People also ask

What does $? In bash mean?

$? $0 is one of the most used bash parameters and used to get the exit status of the most recently executed command in the foreground. By using this you can check whether your bash script is completed successfully or not.

What does $# represent in shell scripting?

$# : This variable contains the number of arguments supplied to the script. $? : The exit status of the last command executed. Most commands return 0 if they were successful and 1 if they were unsuccessful. Comments in shell scripting start with # symbol.

What does #! Mean in shell script?

The shebang must be the first line of the file, and start with " #! ". In Unix-like operating systems, the characters following the " #! " prefix are interpreted as a path to an executable program that will interpret the script.


2 Answers

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

like image 129
Daenyth Avatar answered Oct 10 '22 02:10

Daenyth


-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 the 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.

like image 35
Tejeshreddy Avatar answered Oct 10 '22 03:10

Tejeshreddy