I know how the if shell command is used. But how does it actually work? Everything I found about it online only explains how it is used.
Is it a built in feature of the shell or is it just a regular command that I could write myself too?
if echo "test"; then mkdir one; else mkdir two; fi
Does this call an if command with echo "test" as its arguments? Does it “remember” the result, so that the next time a then or else command is executed, they can decide whether to actually run or not?
Or does the shell parse something like this and directly executes echo "test"? If so, why do we need a terminating ; or \n? Couldn’t it just scan to the then keyword?
if,then, andelseare shell keywords, and are special-cased.It needs the command terminator so that it knows that the arguments to the command have terminated. Otherwise,
thenwill be passed as an argument to the command.