On this page

Making Scripts Executable With a Hashbang (Shebang)

Concepts Jump to heading

  • Deno.env provides environment variables.
  • env runs a program in a modified environment.

Overview Jump to heading

Making Deno scripts executable can come in handy when creating small tools.

Note: Hashbangs do not work on Windows.

Example Jump to heading

In this program, we give the context permission to access the environment variables and print the Deno installation path.

#!/usr/bin/env -S deno run --allow-env

/**
 *  hashbang.ts
 */

const path = Deno.env.get("DENO_INSTALL");

console.log("Deno Install Path:", path);

Permissions Jump to heading

You may need to give the script execution permissions.

Unix Jump to heading

chmod +x hashbang.ts

Execute Jump to heading

Start the script by calling it like any other command.

./hashbang.ts

Details Jump to heading

  • A hashbang has to be placed in the first line.

  • -S splits the command into arguments.

  • End the file name in .ts for the script to be interpreted as TypeScript.

Using hashbang in files with no extension Jump to heading

You may not wish to use an extension for your script's filename. In this case supply one using the --ext flag.

$ cat my_script
#!/usr/bin/env -S deno run --allow-env --ext=js
console.log("Hello!");
$ ./my_script
Hello!