1

A Bundle identifier in Info.plist of an Xcode project could have various forms, for e.g.

  1. com.company.$(PRODUCT_NAME:rfc1034identifier)
  2. $(PRODUCT_BUNDLE_IDENTIFIER)
  3. Someone could design their own product bundle identifier for debug, release etc types of build and write a variable against Bundle identifier, e.g. com.company.$(PRODUCT_NAME:rfc1034identifier).$(someRandomVariable)

I want to write a shell script that just reads the bundle identifier properly.

However, if you only know shell script - I know, how to figure out values of variables in $(), but want a shell script that should give me all such variables in the string and then I will have code to figure out their values, post which I will create string back with the variables replaced with values.

function getBundleIdentifier
{
    cfBundleIdentifier=${PRODUCT_BUNDLE_IDENTIFIER}
    if [ ${#cfBundleIdentifier} -lt 1 ]; then

        SOURCE="rfc1034identifier"
        cfBundleIdentifier=$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "${PROJECT_DIR}/${INFOPLIST_FILE}")

        if echo "$cfBundleIdentifier" | grep -q "$SOURCE"; then
            echo `eval echo $cfBundleIdentifier``eval echo ${PRODUCT_NAME:rfc1034identifier}`
        else
            echo `eval echo $cfBundleIdentifier`
        fi

    else
        echo $cfBundleIdentifier
   fi
}

This is what I have written, but it does not cover all the cases.

prabodhprakash
  • 3,825
  • 24
  • 48

4 Answers4

7

It's very easy, just run this command:

BUNDLE_ID=`xcodebuild -showBuildSettings | grep PRODUCT_BUNDLE_IDENTIFIER`

echo $BUNDLE_ID
loki
  • 9,816
  • 7
  • 56
  • 82
Abouluadi
  • 101
  • 1
  • 4
  • The only caveat to this I found was that it attached 'PRODUCT_BUNDLE_IDENTIFIER = ' before the actual Bundle ID. – Kieran Bond Jan 15 '20 at 11:29
  • @KieranBond True but just do this to get rid of that part: echo $BUNDLE_ID | sed "s/ PRODUCT_BUNDLE_IDENTIFIER = //g" – JeremyF Feb 11 '21 at 22:39
  • I am no genius but sed got me just bundle ID by itself: echo $(xcodebuild -showBuildSettings | grep PRODUCT_BUNDLE_IDENTIFIER) | sed "s/ //g" | sed "s/PRODUCT_BUNDLE_IDENTIFIER//g" | sed "s/=//g" – JeremyF Feb 11 '21 at 22:43
1

you can use this

xcodebuild -project Myproject.xcodeproj \-showBuildSettings | grep PRODUCT_BUNDLE_IDENTIFIER | awk -F ' = ' '{print $2}'
Marwan Alqadi
  • 795
  • 8
  • 14
0

or just consolidated the non-xcode portion into :

xcodebuild……. |

{m,g}awk '$!NF = $(NF=2*/PRODUCT_BUNDLE_IDENTIFIER/)' FS=' = '
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11
0

To get get the bundle identifier in a shell script, I found this to be the simplest way:

xcodebuild -showBuildSettings | awk -F ' = ' '/PRODUCT_BUNDLE_IDENTIFIER/ { print $2 }'

If your script works currently in a different path, use -project option:

xcodebuild -project Myproject.xcodeproj -showBuildSettings | awk -F ' = ' '/PRODUCT_BUNDLE_IDENTIFIER/ { print $2 }'
soundflix
  • 928
  • 9
  • 22