-2

On Linux if-else code is:

if [[ condition ]]
then 
    echo ""
else
    echo ""
fi

How does the above code changes when scripting in MacOS?

1
  • 2
    macOS only provides version 3.2 of bash; most Linux distributions ship a newer version. What is the exact code you are running, and how does its result differ from what you are expecting?
    – chepner
    Jul 11, 2020 at 18:41

2 Answers 2

2

Bash is basically the same on any platform. Your if-else code will be exactly the same on Linux, MacOS or even Windows.

The main differences you could find are:

  • available commands - not every tool is ported to every platform
  • minor features changes due to using older/newer version of shell
  • different files locations
3
  • when I tried the above code in DARWIN is was not getting desirable output Jul 11, 2020 at 18:38
  • 1
    You need to provide better example (in another question), because the code you have provided there will give always empty output Jul 11, 2020 at 18:41
  • 2
    Identical versions of bash are basically the same on any platform. It's well known that the version of bash that ships with macOS is quite a bit older than what you can typically expect from most Linux distributions.
    – chepner
    Jul 11, 2020 at 18:42
0

It does not. On both Linux and Darwin, the statement you have specified returns a continuation prompt (i.e. '> '):

$: if [[ 1 ]] then echo "true" else echo "false" fi

This however, works exactly the same on Linux and MacOS

$ if [ 1 ]; then echo "it is true"; else echo "it is false"; fi it is true

1
  • OP had linefeeds in the original revision, but they got eaten by a lack of code formatting in the post Jul 11, 2020 at 20:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.