0

I have just started writing shell scripts. In if constructs, is the if considered to be a command or a keyword?

If it is a command, its path should be listed when searched through the which command. In reality, which does not find anything.

If if is not a command then ideally it does not need to be separated by a semicolon when then is written in the same line.

Can any of you please explain whether if is a command or a statement?

1
  • It can't be an external command, because it has to affect the behavior of commands and statements that come after it in the script, and also has to match up with the fi keyword. Commented Dec 15, 2013 at 8:54

3 Answers 3

1

In bash if is a compound command (see "Shell Grammar" in the linked page.)

Sign up to request clarification or add additional context in comments.

3 Comments

Yes! Please read the bash man page. It has a lot of information on scripting.
Also, be aware of bash features that are extensions beyond what the POSIX sh (Bourne Shell) supports and mark your scripts as #!/bin/bash scripts if they require such features.
0

if is not a command it is the shell's construct.

Semicolon ; after condition is the shell syntax to terminate the condition and is required only when then keyword is on the same line. ; is not mandatory when then keyword appears on the next line.

So this will also work without semicolon and print date:

if ((RANDOM>0))
then
    date
fi

Comments

0

if is a keyword. The semi-colon terminates the command that is between if and then.

Comments

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.