How to run compiled-language source code files as executable scripts

2 minute read

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:

  1. 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
  2. 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 with sudo apt update && sudo apt install ccache.

  3. 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?

  4. For Rust (compiled)
     ///usr/bin/env rustc "$0" -o /tmp/a && /tmp/a "$@"; exit

    Extra help:

    1. Install Rust:
       curl --proto '=https' --tlsv1.2 -sSf | sh
    2. The official “Rusty By Example” online book “hello world” example:
  5. For Bash (interpreted)
     #!/usr/bin/env bash
  6. 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     # Rust
chmod +x     # Bash
chmod +x     # Python

# run
cd path/to/dir/containing/these/files
./hello_world.c      # C
./hello_world.cpp    # C++
./hello_world.go     # Go
./     # Rust
./     # Bash
./     # Python


  1. You can see and run the above several usages of shebangs for a variety of languages in my eRCaGuy_hello_world repo.
    1. Look for the files named language/hello_world* or language/hello*.
    2. Ex: for C: c/hello_world_extra_basic.c
  2. Shebang starting with //?
  3. [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 or 5 minutes ago (or equivalent) part where it says YOUR_NAME commented just now or YOUR_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.