# Scripting Swift

Swift not only allows you to write compiled applications with or without UI interaction, but also is able to be run as command line shell scripts.

Simply start a new Swift file by adding the usual `#! …` front matter as the first line of your source code and add the path to the location of the `swiftc` command — and you're set. Here is a practical example:

```swift
#!/usr/bin/swift

/*
 Some nice formatting while printing to the console. We use ANSI TTY escape sequences and colour codes here. `CustomStringConvertible` makes the handling of the struct much easier.
 */
enum Colour: String, CustomStringConvertible {
	var description: String {
		self.rawValue
	}

	case reset = "\u{1b}[0m"
	case colourReset = "\u{1b}[39;49m"
	case bold = "\u{1b}[1m"
	case red = "\u{1b}[37;41m"
	case green = "\u{1b}[37;42m"
	case blue = "\u{1b}[37;44m"
}

print("\(Colour.blue)\(Colour.bold) Hello, \(Colour.red) Swift! \(Colour.reset)")
```

Save the file as — for example — `hello_swift.swift` and make it executable by setting the corresponding flag:

```bash
chmod u+x hello_swift.swift
```

You then can execute the Swift script by dotting it like so:

```bash
./hello_swift.swift
```

Please find my Swift scripts in the corresponding [GitHub](https://github.com/MatiMax/swift) repository.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.matimax.info/swift/cli-tools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
