Cross-platform shell scripts in V

V can be used as an alternative to Bash to write deployment scripts, build scripts, etc.

The advantage of using V for this is the simplicity and predictability of the language, and cross-platform support. “V scripts” run on Unix-like systems as well as on Windows.

Use the .vsh file extension. It will make all functions in the os module global (so that you can use ls() instead of os.ls(), for example).

  1. #!/usr/local/bin/v run
  2. // The shebang above associates the file to V on Unix-like systems,
  3. // so it can be run just by specifying the path to the file
  4. // once it's made executable using `chmod +x`.
  5. rm('build/*')
  6. // Same as:
  7. for file in ls('build/') {
  8. rm(file)
  9. }
  10. mv('*.v', 'build/')
  11. // Same as:
  12. for file in ls('.') {
  13. if file.ends_with('.v') {
  14. mv(file, 'build/')
  15. }
  16. }

Now you can either compile this like a normal V program and get an executable you can deploy and run anywhere: v deploy.vsh && ./deploy

Or just run it more like a traditional Bash script: v run deploy.vsh

On Unix-like platforms, the file can be run directly after making it executable using chmod +x: ./deploy.vsh