Rickyleroy

Q: Bash version in to OS X

Can I create Bash script into OS X System and to move it to Linux system using the same shell?

Both system use the same Bash commands?

MacBook Pro with Retina display, OS X Yosemite (10.10.5)

Posted on Nov 28, 2015 12:47 AM

Close

Q: Bash version in to OS X

  • All replies
  • Helpful answers

  • by BobHarris,Apple recommended

    BobHarris BobHarris Dec 3, 2015 8:24 PM in response to Rickyleroy
    Level 6 (19,272 points)
    Mac OS X
    Dec 3, 2015 8:24 PM in response to Rickyleroy

    Yes and no.

     

    They both have bash, however, not all commands are the same between OS X and Linux, and when the commands are the same, not all the options are the same.  But for the most part command options available on OS X will be a subset of the richer set of options on Linux.  There will only be a few commands on Linux that do not accept options available on OS X.

     

    Also OS X's bash is version 3.2.57(1), where as Linux is going to have a bash with a 4.something version.  98% of bash behavior will be the same, but there are going to be a few edge conditions where they may behave differently.

     

    NOTE: Commands are separate from bash.  Each is their own program and are not tied to each other in anyway.

     

    I live in an OS X, Linux, Solaris, AIX Unix world, and for the most part I can write scripts on one platform and run them on any of the others.  Where I have difficulties, I add code along the lines of

     

    UNAME=$(uname)

    if [[ $UNAME = "Linux"  ]]; then

        ... Linux specific

    elif [[ $UNAME = Darwin ]]; then

        ... OS X specific

    elif [[ $UNAME = AIX ]]; then

        ... AIX specific

    else

        ... generic ...

    fi

     

    The reason OS X has an older version of bash, and why OS X does not include the Linux commands with all the extra options is that many of those programs have a GPL V3 license that explicitly excludes commercial Unix vendors from including them in their operating system distributions, so the commercial Unix companies are stuck with either GPL V2 licensed code, or code they got from BSD distributions, such as FreeBSD (a lot of OS X command are originally from FreeBSD).

     

    However, if there is an open source command that you really want on your system, you are allowed to install it yourself.  I actually build and install my own current version of bash on my OS X systems.  The GPL V3 license does not restrict you, only Apple.

     

    There are several package managers that make adding open source commands fairly easy.  Homebrew, MacPorts, Fink.

  • by Skybridge Translation,

    Skybridge Translation Skybridge Translation Jun 14, 2016 11:55 AM in response to Rickyleroy
    Level 1 (4 points)
    Jun 14, 2016 11:55 AM in response to Rickyleroy

    Hi Ricky LeRoy,

     

    Yeah of course.  Just depends on what Linux system you use. 

     

    If you're writing something simple like this:

     

    #!/bin/bash

     

    echo -e "Please enter your name: "

    read name

    echo "Nice to meet you $name"

     

    It should work on any system.

     

    If your script is calling various Unix / Linux tools that are found in other directories, then you'll have to tweak your program to point to those directories.

    Each version of Linux has it's own directory layout.  Programs are found in different directories. 

     

    Mich (Skybridge Translation)