Compile and Run commands for swift
Open the terminal and type:
sudo apt install clang libicu-dev -y
wget https://swift.org/builds/swift-4.2.3-release/ubuntu1804/swift-4.2.3-RELEASE/swift-4.2.3-RELEASE-ubuntu18.04.tar.gz
mkdir ~/swift
tar -xvzf swift-4.2.3-RELEASE-ubuntu18.04.tar.gz -C ~/swift
nano ~/.bashrc
.bashrc will open in the terminal for editing in nano text editor. Paste the following line at the end of .bashrc.
export PATH=~/swift/swift-4.2.3-RELEASE-ubuntu18.04/usr/bin:$PATH
Press the keyboard combination Ctrl+O and after that press Enter to save the file being edited. Press the keyboard combination Ctrl+X to exit nano.
Close the terminal, open a new terminal, and run the following commands.
swift -version # This command should print Swift version 4.2.3.
cd Desktop/
mkdir helloworld-project && cd helloworld-project
swift package init --type executable
swift build
.build/debug/helloworld-project
Results:
Hello, world!
Swift REPL example 1
Swift has an interactive interpreter called the REPL which stands for Read-Eval-Print-Loop. The REPL can be run interactively from the command line as demonstrated in the below example.
$ swift -repl :0: warning: unnecessary option '-repl'; this is the default for 'swift' with no input files Welcome to Swift version 4.2.3 (swift-4.2.3-RELEASE). Type :help for assistance. 1> // Hello, World! Program 2> import Swift 3> print("Hello, World!") Hello, World! 4> :exit
Swift REPL example 2
This example executes the same code as example 1 with two differences.
- Everything is done in Gedit.
- The entire code block is copy/pasted from the edit pane into the terminal pane in Gedit (Embedded Terminal plugin), and run by the Swift interpreter as a block of code instead of one line at a time.