Logo for tanaschita.com

Introduction to bash scripting for iOS developers

Learn how to use bash scripting for iOS development.

23 Oct 2023 · 6 min read

Bash (or shell) scripting provides a powerful way to automate repetitive tasks and can save us developers a lot of time.

Using Bash scripting as an iOS developer can help perform a wide range of tasks such as automating build processes, managing file systems, running tests or and deploying applications. Any command we can run in the terminal can be run in a Bash script.

In this article, we'll look at the basics of Bash scripting and a practical example for iOS development.

Let's get started.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

What is Bash?

Bash, short for Bourne Again Shell, is a command-line shell and scripting language widely used in Unix and Linux environments. It provides a way to interact with the operating system by executing commands and scripts.

Writing and running your first Bash script

Every Bash script file starts with #!/bin/bash on its own line. This way, the computer knows which type of interpreter to use for the script.

#!/bin/bash
echo "Hello world!"

We can save the code above in a file with a .sh extension, for example test.sh and then try and run it in the terminal by simply entering the path to the script:

/path/test.sh

A script file needs to have permission to be executed. In case of a permission error, run the following command:

chmod +x /path/test.sh

After running the script again, the Hello world! greeting should now be printed out in the terminal.

Basic scripting concepts

While Bash is primarily a scripting language, it incorporates many programming language elements. Here are some of the key ones:

1. Variables

In Bash scripting, variables are declared and used as follows:

#!/bin/bash
hello="Hello to you!"
echo $hello

As shown above, when accessing a value of a variable, we always prefix it with a $ sign.

2. Conditionals

Bash scripting supports if-else conditions:

if [ "$input" == "$name" ]
then
echo "yes"
else
echo "no"
fi

When working strings, it's best practice to put the variable into quotes to prevent errors when the variable contains spaces.

3. Loops

Bash scripts support for, while and until loops. Let's look at a for-loop example:

for name in 1 2 3 4 5
do
echo $name
done

Alternatively to the sequence, we could setup a range {1..5} to achieve the same results.

4. Functions

Bash also supports functions:

function greet() {
echo "Hello, $1!"
}
greet "Alice"

Function arguments are not defined as part of a function but accessed using $1, $2 etc.

There is more to discover like working with arrays and dictionaries, numeric calculations, error handling, file handling and more which is not covered by this article. Checkout this cheatsheet on Bash scripting to see more examples.

Handling user input

When writing a bash script, we often need some kind of outside input. A common way is to use input arguments that are entered when calling a script separated by spaces:

/path/test.sh argument1 argument2

Within the script, the arguments are accessed using $1, $2 etc.

Another possibility is to request user input with read:

echo "What's your name?"
read userInput
echo $userInput

Setup an alias

When working with a script, we can setup an alias so we don't need to use the full path every time we call it:

alias test='/path/test.sh'

Now we can call the script by using the alias:

testscript argument1 argument2

For a permanent alias, we can set it up in the .bash_profile file which is a hidden configuration file in the home directory.

Bash scripting for iOS example

Finally, let's look at a practical example on how we could use bash scripting for iOS development.

The following script automates taking screenshots of an iOS application for different devices and languages:

#!/bin/bash
// 1.
projectName="./exampleApp.xcodeproj"
schemeName="exampleApp"
simulators=("iPhone 14" "iPhone 13")
languages=("en" "de")
appearances=("light" "dark")
// 2.
targetFolder=$1
// 3.
for simulator in "${simulators[@]}"
do
for language in "${languages[@]}"
do
for appearance in "${appearances[@]}"
do
// 4.
xcrun simctl boot "$simulator"
xcrun simctl ui "$simulator" appearance $appearance
xcodebuild -testLanguage $language -scheme $schemeName -project $projectName -destination "platform=iOS Simulator,name=$simulator" build test
xcparse screenshots /path/to/xcresult "$targetFolder/$simulator/$language/$appearance" \;
done
done
done

Let's go through it step-by-step:

  1. Defining variables that list devices, languages and appearance modes we'll use to take screenshots.
  2. Defining the targetFolder variable and assigning it the first user input argument.
  3. Looping through previously defined variables.
  4. Using xcrun, xcodebuild and xcparse command line tools to start a simulator, run tests and to extract screenshots that were taken while running the tests.

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

swiftdata

swift

ios

How to define one-to-many relationships in SwiftData

Learn to use SwiftData's @Relationship macro.

19 Feb 2024 · 4 min read

Latest articles and tips

© 2024 tanaschita.com

Privacy policy

Impressum