Skip to content

Commit 2fcf5d4

Browse files
authored
docs: add instructions for running Vala programs as scripts (#90)
Signed-off-by: Zhou Qiankang <wszqkzqk@qq.com>
1 parent 07c0fe6 commit 2fcf5d4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

source/tutorials/programming-language/main/01-00-first-program.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,34 @@ Assuming you have Vala installed, then all it takes to compile and execute this
6666

6767
If you get some warnings from a C language compiler, please jump to :doc:`07-00-tools/07-01-valac` for the reason and solution.
6868

69+
Running as a Script
70+
-------------------
71+
72+
On Unix-like operating systems, you can also run a Vala source file directly as a script. This is achieved by adding a 'shebang' line at the very beginning of the file. For example, you could save the "Hello, World" program as ``hello.vala`` with the following content:
73+
74+
.. code-block:: vala
75+
:emphasize-lines: 1
76+
77+
#!/usr/bin/env vala
78+
class Demo.HelloWorld : GLib.Object {
79+
public static int main(string[] args) {
80+
stdout.printf("Hello, World\n");
81+
return 0;
82+
}
83+
}
84+
85+
The first line, ``#!/usr/bin/env vala``, is the shebang. It tells the system to use the ``vala`` command to execute this file.
86+
87+
Before running it, you need to grant the file executable permissions:
88+
89+
.. code-block:: console
90+
91+
$ chmod +x hello.vala
92+
93+
Now you can run it directly from your terminal:
94+
95+
.. code-block:: console
96+
97+
$ ./hello.vala
98+
99+
This will produce the same "Hello, World" output. Behind the scenes, the system invokes the Vala compiler to compile the source code into a temporary binary file, and then executes that binary.

0 commit comments

Comments
 (0)