Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/lib/engine/shell/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,7 @@ async function echoCommand(args: string[], fs: FsLike, cwd: string): Promise<Bui
const escapeFlag = raw.startsWith('-e ');
const content_raw = escapeFlag ? raw.slice(3) : raw;

// Handle redirect: echo [-e] "content" > file
const redirectMatch = content_raw.match(/^(.*?)\s*>\s*(.+)$/);
if (redirectMatch) {
let content = redirectMatch[1].replace(/^["']|["']$/g, '');
if (escapeFlag) content = interpretEscapes(content);
const filepath = resolvePath(redirectMatch[2].trim(), cwd);
try {
await fs.promises.writeFile(filepath, content + '\n');
return { output: '', success: true };
} catch {
return { output: `echo: cannot write to '${redirectMatch[2]}'`, success: false };
}
}

// Handle append: echo [-e] "content" >> file
// Handle append: echo [-e] "content" >> file (must check before > to avoid >> being parsed as >)
const appendMatch = content_raw.match(/^(.*?)\s*>>\s*(.+)$/);
if (appendMatch) {
let content = appendMatch[1].replace(/^["']|["']$/g, '');
Expand All @@ -189,6 +175,20 @@ async function echoCommand(args: string[], fs: FsLike, cwd: string): Promise<Bui
}
}

// Handle redirect: echo [-e] "content" > file
const redirectMatch = content_raw.match(/^(.*?)\s*>(?!>)\s*(.+)$/);
if (redirectMatch) {
let content = redirectMatch[1].replace(/^["']|["']$/g, '');
if (escapeFlag) content = interpretEscapes(content);
const filepath = resolvePath(redirectMatch[2].trim(), cwd);
try {
await fs.promises.writeFile(filepath, content + '\n');
return { output: '', success: true };
} catch {
return { output: `echo: cannot write to '${redirectMatch[2]}'`, success: false };
}
}

let output = content_raw.replace(/^["']|["']$/g, '');
if (escapeFlag) output = interpretEscapes(output);
return { output, success: true };
Expand Down