Skip to content

Instantly share code, notes, and snippets.

@kavu
Last active June 20, 2017 02:58
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kavu/79f05be2383e97843867 to your computer and use it in GitHub Desktop.
Save kavu/79f05be2383e97843867 to your computer and use it in GitHub Desktop.
Minimal Swift command line Hello World
#!/bin/sh
# So you've installed XCode 6 Beta
# Now we could use Swift toolchain to build a minimal
# command line Hellow World
# let's set new Developer Toolchain bundled with Xcode6-Beta.app
# as default toolchain
# sudo xcode-select -s /Applications/Xcode6-Beta.app/Contents/Developer
# alias for Swift binary
# export SWIFT=/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
# simple program
#echo 'println("Hello World")' > swifthello.swift
# build stuff
#$SWIFT -sdk $(xcrun --show-sdk-path --sdk macosx) swifthello.swift
## Thanks to @caius, lines 10-17 now obsolete. Just use `xcrun'
echo 'println("Hello World")' > swifthello.swift
xcrun swift swifthello.swift
# run stuff
./swifthello
# By the way, Swift had a lot of options, invoke with `xcrun swift --help'
# Hope that helps
@kirs
Copy link

kirs commented Jun 3, 2014

I would suggest to make symlink instead export SWIFT:

ln -s /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift /usr/local/bin/swift`

@mattspatola
Copy link

Or even just add that location to your PATH if you want the rest of it.

@caius
Copy link

caius commented Jun 3, 2014

xcrun(1) exists for exactly this reason, and negates the -sdk flag being needed as well. You can replace lines 10 through 17 with just:

echo 'println("Hello World")' > swifthello.swift
xcrun swift swifthello.swift

@tabuchid
Copy link

tabuchid commented Jun 4, 2014

You can also use the DEVELOPER_DIR environment variable if you don't want to globally change you're toolchain.
DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer/ xcrun swift'

@jkleiser
Copy link

jkleiser commented Jun 6, 2014

Thanks to a recent tweet from @SwiftDevs I also learned that a .swift file like this can be run as a script, i.e. without explicit compilation, e.g. by including a hash-bang line like this:

!/usr/bin/xcrun swift -i

@qserve
Copy link

qserve commented Jun 24, 2014

Yes it can, but implicitly the code is compiled.

"The Swift REPL (Read-Eval-Print-Loop) acts like an interpreter. Valid statements, expressions, and declarations are immediately compiled and executed."

@devsmt
Copy link

devsmt commented Aug 19, 2015

to produce an actual executable:

swiftc script.swift -o myprogram
./myprogram

@JoshCheek
Copy link

When I tried this, it couldn't find println, but I swapped it with print and it worked. Also note that I seem to have swift as a binary in /usr/bin/swift, so for me it was just echo 'print("Hello World")' > hw.swift && swift hw.swift. Amusingly, since running swift without args starts a repl, which reads from stdin, I was also able to echo 'print("Hello World")' | swift

@stanwu
Copy link

stanwu commented Jun 4, 2017

Below example is more simpler :

hello.swift

print("Hello World");

then

swift hello.swift

output

Hello World

@johnutz-self
Copy link

or you could do this:

echo print("Yo Planet") | swift

Welcome to Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42). Type :help for assistance.
Yo Planet

Can't figure out how to suppress the welcome blurb, there is no -q (aka quiet) argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment