How to run compiled-language source code files as executable scripts
Hash-bang/shebang examples for a variety of languages, to run them as executable files
You can run even compiled languages as though they were executable scripts.
Here are the necessary hash-bang lines I like to use as the first line at the top of my programs, in order to make this possible:
- For C (compiled) (technically: gnu C as I’ve specified it below):
///usr/bin/env ccache gcc -Wall -Wextra -Werror -O3 -std=gnu17 "$0" -o /tmp/a -lm && /tmp/a "$@"; exit
- For C++ (compiled) (technically: gnu++ as I’ve specified it below):
///usr/bin/env ccache g++ -Wall -Wextra -Werror -O3 -std=gnu++17 "$0" -o /tmp/a -lm && /tmp/a "$@"; exit
For C and C++,
ccache
helps ensure your compiling is a little more efficient. Install it in Ubuntu withsudo apt update && sudo apt install ccache
. - For Go (compiled) (golang)
///usr/bin/env go run "$0" "$@"; exit
…and for more explanations of the lines above, see my other answer here: What’s the appropriate Go shebang line?
- For Rust (compiled)
///usr/bin/env rustc "$0" -o /tmp/a && /tmp/a "$@"; exit
Extra help:
- Install Rust: https://www.rust-lang.org/tools/install
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- The official “Rusty By Example” online book “hello world” example: https://doc.rust-lang.org/stable/rust-by-example/hello.html
- Install Rust: https://www.rust-lang.org/tools/install
- For Bash (interpreted)
#!/usr/bin/env bash
- For Python (interpreted)
#!/usr/bin/env python3
Prefix with time
if you wish
If you’d like to time the compilation time, add bash -c time
near the beginning of the hash-bang, as follows. Here is an example for C++ (technically gnu++17 as I’ve written it):
///usr/bin/env bash -c time ccache g++ -Wall -Wextra -Werror -O3 -std=gnu++17 "$0" -o /tmp/a -lm && /tmp/a "$@"; exit
Now you can mark the files as executables and run them directly!
# make executable
chmod +x hello_world.c # C
chmod +x hello_world.cpp # C++
chmod +x hello_world.go # Go
chmod +x hello_world.rs # Rust
chmod +x hello_world.sh # Bash
chmod +x hello_world.py # Python
# run
cd path/to/dir/containing/these/files
./hello_world.c # C
./hello_world.cpp # C++
./hello_world.go # Go
./hello_world.rs # Rust
./hello_world.sh # Bash
./hello_world.py # Python
References:
- You can see and run the above several usages of shebangs for a variety of languages in my eRCaGuy_hello_world repo.
- Look for the files named
language/hello_world*
orlanguage/hello*
. - Ex: for C: c/hello_world_extra_basic.c
- Look for the files named
- Shebang starting with
//
? - [my answer] What’s the appropriate Go shebang line?
I also posted this as an answer on Stack Overflow here: Run C or C++ file as a script.
Leave a comment
Comments are powered by Utterances. A free GitHub account is required. Comments are moderated. Be respectful. No swearing or inflammatory language. No spam.
I reserve the right to delete any inappropriate comments. All comments for all pages can be viewed and searched online here.
To edit or delete your comment: Option 1 (recommended): click the date just above your comment, ex: the
just now
or5 minutes ago
(or equivalent) part where it saysYOUR_NAME commented just now
orYOUR_NAME commented 5 minutes ago
, etc., or Option 2: click the "Comments" link at the top of the comments section below where it says how many comments have been left. Option 1 will take you directly to your comment on GitHub. Option 2 will take you to a GitHub page with all comments for this page. Then: --> find your comment on this GitHub page and click the 3 dots in the top-right of your comment --> click "Edit" or "Delete". Editing or adding a comment from the GitHub page also gives you a nicer editor.