0

I am trying to check if multiple command line arguments are set. Eg.

if [-n "$1"] && [-n "$2"] && [-n "$3"]; then
   do something
else
   do something else
fi

But I keep getting

bash: [-n: command not found

What's the correct way to do this?

1 Answer 1

4

You need spaces between the braces, i.e.

if [ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ]; then

You can also use extended test command ([[...]]) as

if [[ -n "$1" && -n "$2" && -n "$3" ]]; then
Sign up to request clarification or add additional context in comments.

2 Comments

wow I wouldn't have spotted that one thanks! will accept when it lets me
@PaulStatham: You can always use shellcheck.net to debug such syntax issues in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.