Shell
访问系统 shell。允许你生成子进程。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | | Only allows to open URLs via |
| ios | | Only allows to open URLs via |
如果你正在查找 shell.open 接口的文档,请查看新的指定打开应用插件。
请从安装 shell 插件开始。
使用项目的包管理器来添加依赖。
npm run tauri add shellyarn run tauri add shellpnpm tauri add shelldeno task tauri add shellbun tauri add shellcargo tauri add shell-
在
src-tauri目录下,运行下面的命令。将此插件添加到项目的Cargo.toml文件的dependencies字段中。cargo add tauri-plugin-shell -
修改
lib.rs来初始化插件。src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_shell::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你喜欢的 JavaScript 包管理器安装 JavaScript Guest 绑定。
npm install @tauri-apps/plugin-shellyarn add @tauri-apps/plugin-shellpnpm add @tauri-apps/plugin-shelldeno add npm:@tauri-apps/plugin-shellbun add @tauri-apps/plugin-shell
Shell 插件有 JavaScript 和 Rust 两种版本。
import { Command } from '@tauri-apps/plugin-shell';// 当设置 `"withGlobalTauri": true`, 你可以使用// const { Command } = window.__TAURI__.shell;
let result = await Command.create('exec-sh', [ '-c', "echo 'Hello World!'",]).execute();console.log(result);use tauri_plugin_shell::ShellExt;
let shell = app_handle.shell();let output = tauri::async_runtime::block_on(async move { shell .command("echo") .args(["Hello from Rust!"]) .output() .await .unwrap()});if output.status.success() { println!("Result: {:?}", String::from_utf8(output.stdout));} else { println!("Exit with code: {}", output.status.code().unwrap());}默认情况下,所有具有潜在危险的插件命令和范围都会被阻止且无法访问。您必须修改 capabilities 文件夹中的配置来启用它们。
参见能力概览以获取更多信息,以及插件的分步导览来调整插件权限。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "shell:allow-execute", "allow": [ { "name": "exec-sh", "cmd": "sh", "args": [ "-c", { "validator": "\\S+" } ], "sidecar": false } ] } ]}Default Permission
This permission set configures which shell functionality is exposed by default.
Granted Permissions
It allows to use the open functionality with a reasonable
scope pre-configured. It will allow opening http(s)://,
tel: and mailto: links.
This default permission set includes the following:
allow-open
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the execute command without any pre-configured scope. |
|
|
Denies the execute command without any pre-configured scope. |
|
|
Enables the kill command without any pre-configured scope. |
|
|
Denies the kill command without any pre-configured scope. |
|
|
Enables the open command without any pre-configured scope. |
|
|
Denies the open command without any pre-configured scope. |
|
|
Enables the spawn command without any pre-configured scope. |
|
|
Denies the spawn command without any pre-configured scope. |
|
|
Enables the stdin_write command without any pre-configured scope. |
|
|
Denies the stdin_write command without any pre-configured scope. |
© 2025 Tauri Contributors. CC-BY / MIT