Writing linux scripts in C++
They say compiled languages are inconvenient for scripting because you have to recompile your script everytime you edit it. But you don't have to do it manually. Here's the solution for C++.
Disclaimer. Don't tell me writing scripts in C++ is bad idea. Don't be dogmatic. All rules have exceptions.
In bash scripts, first line always starts with "shebang" followed by name of a bash interpreter. When you run bash script, effectively bash interpreter is started with script's filename in first command-line argument.
#!/bin/bash echo "Hello world!"
Same approach is used for Python scripts, Scala scripts, etc. And I did just the same.
Clone or download this repo.
Build and install
make && sudo make install. It goes into
Create C++ source file, prepend it with shebang line
#!/usr/local/bin/build-n-run, make it executable
chmod +x script.cpp. See
You can specify custom build command in script's 2nd line.
~into current user's home directory in
Run your script:
build-n-runwill compile it on first run and after everytime you edit / touch it.
/aaa/bbb/ccc.cpp is compiled into
~/.cache/build-n-run/aaa--bbb--ccc. If compilation fails,
build-n-run will exit with status code
ATTENTION. Compiler's output is not redirected. So if compilation succeeded but with warnings, those warnings will still go to
and will prepend your script's output.