Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

What is "-n" in shell?

Writer Matthew Martinez

For most of you it might be an easy question. However, I'm just new to shell scripting and somewhere I saw this:

#!/bin/sh
X=0
while [ -n "$X" ]
do echo "Enter some text (RETURN to quit)" read X echo "You said: $X"
done

I didn't understand what is -n here. I google, but couldn't find anything about -n. Any suggestion will be appreciated.

1

1 Answer

-n is an argument to [, which is the name of command used here. Or actually an alias to the test command which additionally requires a matching ] (which is an argument to ] too - confusing, I know).

The Wikipedia article on test lists all its options, including -n:

-n String1 - the length of the String1 variable is nonzero

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy