1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2025 xiefujin <490021684@qq.com>
// Licensed under Apache-2.0, see LICENSE file for full license terms.
use crate::shell::{CommandOutput, Shell};
impl Shell {
pub fn cmd_export(&mut self, args: &[&str]) -> CommandOutput {
if args.is_empty() {
// export -p: list exported variables
let mut out = String::new();
for (k, v) in &self.vars {
if self.exported.contains(k) {
out.push_str(&format!("export {}='{}'\n", k, v));
}
}
return CommandOutput::success(out);
}
if args[0] == "-p" {
let mut out = String::new();
for (k, v) in &self.vars {
if self.exported.contains(k) {
out.push_str(&format!("export {}='{}'\n", k, v));
}
}
return CommandOutput::success(out);
}
if args[0] == "-n" {
// export -n KEY: remove export attribute
for &name in &args[1..] {
self.exported.remove(name);
}
return CommandOutput::success(String::new());
}
if args[0] == "-f" {
// `export -f NAME` marks functions for export. fastshell has no real
// child processes; `sh -c` already sees in-process functions, so we
// just record the names (and never error).
for &name in &args[1..] {
self.exported_functions.insert(name.to_string());
}
return CommandOutput::success(String::new());
}
// export KEY (no =) — mark existing var as exported
// export KEY=val — handled by consume_assignments, but called here too
for &arg in args {
if let Some(eq) = arg.find('=') {
let name = &arg[..eq];
let value = &arg[eq + 1..];
self.vars.insert(name.to_string(), value.to_string());
self.exported.insert(name.to_string());
} else {
// Mark existing variable as exported
if self.vars.contains_key(arg) {
self.exported.insert(arg.to_string());
} else {
// Variable not set yet, mark as exported with empty value
self.exported.insert(arg.to_string());
}
}
}
CommandOutput::success(String::new())
}
}