Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
⌘K
Up or down tonavigateEnter toselectEscape toclose
On this page

Executable scripts

Making Deno scripts executable can come in handy when creating small tools orutilities for tasks like file manipulation, data processing or repetitive tasksthat you might want to run from the command line. Executable scripts allow youto create ad-hoc solutions without setting up an entire project.

Creating an example scriptJump to heading

To make a script executable, start the script with a hashbang, (sometimes calleda shebang). This is a sequence of characters (#!) that tells your operatingsystem how to execute a script. It is followed by the path to the interpreterthat should be used to run the script.

Note

To use a hashbang on Windows you will need to install the Windows Subsystem forLinux (WSL) or use a Unix-like shell likeGit Bash.

We'll make a simple script that prints the Deno installation path using theDeno.env API.

Create a file namedhashbang.ts with the following content:

hashbang.ts
#!/usr/bin/env -S deno run --allow-envconst path= Deno.env.get("DENO_INSTALL");console.log("Deno Install Path:", path);

This script tells the system to use the deno runtime to run the script. The -Sflag splits the command into arguments and indicates that the following argument(deno run --allow-env) should be passed to the env command.

The script then retrieves the value associated with the environment variablenamedDENO_INSTALL withDeno.env.get() and assigns it to a variable calledpath. Finally, it prints the path to the console usingconsole.log().

Execute the scriptJump to heading

In order to execute the script, you may need to give the script executionpermissions, you can do so using thechmod command with a+x flag (forexecute):

chmod +x hashbang.ts

You can execute the script directly in the command line with:

./hashbang.ts

Using hashbang in files with no extensionJump to heading

For brevity, you may wish to omit the extension for your script's filename. Inthis case, supply one using the--ext flag in the script itself, then you canrun the script with just the file name:

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

🦕 Now you can directly execute Deno scripts from the command line! Remember toset the execute permission (chmod +x) for your script file, and you’re all setto build anything from simple utilities to complex tools. Check out theDeno examples for inspiration on what you can script.

Did you find what you needed?

What can we do to improve this page?

If provided, you'll be @mentioned in the created GitHub issue

Privacy policy

[8]ページ先頭

©2009-2025 Movatter.jp