10

I first learned how to code in a C language, and using

if ( code )
then 
code
fi

makes it really hard for me to read my code (don't ask why). Is there a substitute for then?

Something like this:

if ( code ) {
 code
}
fi
1
  • The answers below might help, but if I might add, it is a very good idea to get used to syntax of different languages. If you want to find employment in software development, or devops or as a sysadmin for that matter, you are going to need to learn to recognize and use many different syntaxes. You will be more valuable to any employer by being as flexible as possible.
    – OldTimer
    Commented Jul 22, 2015 at 18:07

6 Answers 6

21

There's no substitute, but you can use both if you really need the braces:

if true ; then {
    something
} fi

That uses an unnecessary explicitly-braced command group as the body of the then block. Any commands in the braces will just be executed in the current shell when the then runs, exactly as though the braces weren't there.

I wouldn't really recommend this style in general or for code that someone else will need to read, but it does give you the braces you want, and it'll do the trick if you're using an editor that requires braced blocks or something like that.

1
  • Here is another which is "not worth recommending" – braces galore: { if { [[ a == aa ]];};then { echo yes;};else { echo no;}; fi;} – basically, when you use bash enough, the standard syntax becomes familiar enough, so that it isn't much of an issue.
    – Peter.O
    Commented Jun 25, 2015 at 8:07
15

You seem to be looking for the C shell (csh, tcsh, etc...). Be aware that experts recommend against it for programming.

I'm not sure whether or not the part that you consider to be unappealing is the fact that then is on the following line? If so, consider using the following style:

if condition; then
    stuff
fi

I have to admit that spelling statement names backwards like fi for if and esac for case (but not elihw for while!) is kind of quirky. But that's standard Bourne shell, the most portable shell syntax you can use.

4
  • Thanks for your answer - I don't find the then unappealing, it's just that my editor will highlight corresponding {} pairs, and that's useful for when I have some sort of error because then I can easily see all of my if statements and see if something went wrong. Commented Jun 25, 2015 at 5:37
  • 1
    Ah yes. That plus vi's % command which jumps to the matching brace/parenthesis/bracket. I sympathize. I miss vi's % command when programming in Python.
    – Celada
    Commented Jun 25, 2015 at 5:42
  • 8
    Have you tried the matchit vim plugin ? It supports the % jump for if/fi in bash and a lot of other blocks in a lot of other languages. See here - vim.org/scripts/script.php?script_id=39
    – amisax
    Commented Jun 25, 2015 at 5:49
  • @AmitKumar thanks for the tip. I'll have a look!
    – Celada
    Commented Jun 25, 2015 at 5:53
9

Maybe this is easier on the eyes:

[ condition ] && {
   # statement 1
   # statment 2
   # ...
}

Other than that, just get used to it. Use the language's idioms.

1
  • 1
    But lose the usually unnecessary bashism and just use one set of brackets. Commented Jun 25, 2015 at 14:01
5

The following is near to there. And weird.

: if; { code; } && {
    code
}

I personally never liked the...

if true; then
more;fi

...form myself. I like it when the control words lead. It always seemed strange to me that the prevailing ...;then form came about at all.

if    this && that
then  do these things too
else  i will probably get upset
fi

It's almost entirely opposite to your request, but it's another way to do it. You can also tack any wanted redirects onto the fi line as needed and they will apply to the whole block.

7
  • This prevailing form seems to be just another incarnation of the common if(...) {, i.e. leaving opening brace on the same line as if in C-like languages.
    – Ruslan
    Commented Jun 25, 2015 at 12:55
  • @Ruslan - Perhaps - but the line doesn't actually make any difference at all in C, does it?
    – mikeserv
    Commented Jun 25, 2015 at 12:57
  • Right, but neither does the line break make a difference in shell, does it? The semicolon after condition already makes the interpreter look for continuation of the if command, even if on the next line.
    – Ruslan
    Commented Jun 25, 2015 at 12:57
  • @Ruslan - yeah - you have to use the ; delim in that case. It's why they do.
    – mikeserv
    Commented Jun 25, 2015 at 12:59
  • Using the : shell builtin like this, just so a line of code could be byte-wise closer to C syntax, should not be encouraged in any way. Commented Oct 31, 2015 at 14:50
0

Here is a way:

[ "$var" = "find" ] && echo "found"

is equivalent to:

if [ "$var" = "find" ]; then
  echo "found"
fi
1
  • As he states in the question, he wants brackets. Put some {...} around the echo "found", and then you'll be heading in the right direction.
    – Peter.O
    Commented Jun 25, 2015 at 7:53
-2

I always use:

if (code); then
   code;
fi

Probably not exactly what you want but it's a bit more c like.

1
  • 2
    That causes a subshell, lowering performance.
    – Will
    Commented Jun 25, 2015 at 5:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .