reef-shell 0.3.0

Bash compatibility layer for fish shell
Documentation
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! Fast bash detection heuristic.
//!
//! Runs on every Enter keypress — zero allocation, no regex, no parsing.
//! Scans raw bytes for bash-specific patterns and triggers.

/// Quick check: does this string contain bash-specific syntax?
/// This must be FAST — it runs on every Enter keypress.
/// No regex, no parsing — just byte-level pattern scanning.
///
/// # Examples
///
/// ```
/// use reef::detect::looks_like_bash;
///
/// assert!(looks_like_bash("if [[ $x == 1 ]]; then echo yes; fi"));
/// assert!(looks_like_bash("export FOO=bar"));
/// assert!(!looks_like_bash("echo hello"));
/// ```
#[must_use]
pub fn looks_like_bash(input: &str) -> bool {
    let bytes = input.as_bytes();
    let len = bytes.len();

    // Single pass: check 2-byte trigger patterns and set flags for slower checks.
    // Most fish commands bail here immediately (no trigger bytes at all).
    let mut has_keyword_char = false;
    let mut has_brace = false;
    let mut has_eq = false;
    let mut has_paren = false;
    let mut in_dquote = false;
    let mut i = 0;
    while i < len {
        let b = bytes[i];
        let next = if i + 1 < len { bytes[i + 1] } else { 0 };
        match b {
            // Track double-quote state so we don't treat ' inside "..." as
            // a single-quote delimiter (e.g. "it's $((2+2)) o'clock").
            b'\\' if in_dquote => { i += 2; continue; }
            b'"' if !in_dquote => { in_dquote = true; i += 1; continue; }
            b'"' if in_dquote => { in_dquote = false; i += 1; continue; }
            // Skip single-quoted sections — everything is literal inside.
            // Prevents false positives like awk '{print $1}'.
            // But inside double quotes, ' is just a literal character.
            b'\'' if !in_dquote => {
                // $'...' (ANSI-C quoting) IS bash-specific.
                if i > 0 && bytes[i - 1] == b'$' {
                    return true;
                }
                i += 1;
                while i < len && bytes[i] != b'\'' {
                    i += 1;
                }
            }
            b'`' => return true,
            // $( alone is valid fish 3.4+ command substitution — don't trigger.
            // $(( is bash arithmetic expansion — not valid fish.
            b'$' => match next {
                b'{' | b'$' | b'#' | b'?' | b'!' | b'0'..=b'9' | b'@' | b'*' => return true,
                b'(' if i + 2 < len && bytes[i + 2] == b'(' => return true,
                _ => {}
            },
            b'<' if matches!(next, b'<' | b'(') => return true,
            b'>' if next == b'(' => return true,
            b'[' if next == b'[' => return true,
            b'(' if next == b'(' && (i == 0 || bytes[i - 1] != b'$') => return true,
            b'(' => has_paren = true,
            b'=' => has_eq = true,
            b'{' => has_brace = true,
            b' ' | b';' | b'\t' | b'\n' => has_keyword_char = true,
            _ => {}
        }
        i += 1;
    }

    // Bash-specific syntax at command position: NAME=, NAME+=, NAME[..]=,
    // NAME(), ( subshell, or { brace group.
    if (has_eq || has_paren || has_brace) && has_bash_cmd_start(bytes) {
        return true;
    }

    // Bash-only variable names: $RANDOM, $SECONDS, etc.
    // Fish doesn't have these as built-in variables.
    if has_bash_var(bytes) {
        return true;
    }

    // Bash-only fd redirections: fd number >= 3 followed by > or <.
    // Fish supports 0<, 1>, and 2> natively; anything higher is bash-only.
    // Catches: 3>&1, 4>&2, 5>/dev/null, etc.
    if has_bash_fd_redirect(bytes) {
        return true;
    }

    // Keyword-based checks — only if separator chars were seen.
    if has_keyword_char {
        // Substring indicators with enough built-in context to avoid false positives.
        const INDICATORS: &[&str] = &[
            "export ",
            "unset ",
            "declare ",
            "typeset ",
            "readonly ",
            "local ",
            " do ",
            ";do ",
            "do\n",
            "do;",
            "shopt ",
            "read -p",
            "read -r",
            "for ((",
            "trap ",
            "eval ",
            "select ",
            "getopts ",
        ];
        // Control-flow keywords checked with word boundaries to avoid
        // false positives (e.g. " fi" inside "file", " done" in "done!").
        const BOUNDARY_KEYWORDS: &[&[u8]] = &[
            b"fi", b"esac", b"let",
        ];
        for kw in INDICATORS {
            if input.contains(kw) {
                return true;
            }
        }
        for kw in BOUNDARY_KEYWORDS {
            if has_word(bytes, kw) {
                return true;
            }
        }
    }

    // Brace range expansion: {1..5}, {a..z}, {1..10..2} — needs quote-aware scan
    if has_brace && has_brace_range(bytes) {
        return true;
    }

    false
}

/// Check for bash-only variable references like `$RANDOM`, `$SECONDS`, etc.
/// Requires a word boundary after the name to avoid matching `$RANDOM_SEED`.
fn has_bash_var(bytes: &[u8]) -> bool {
    const BASH_VARS: &[&[u8]] = &[
        b"BASH_VERSION", b"BASH_REMATCH", b"BASH_SOURCE",
        b"RANDOM", b"SECONDS", b"LINENO", b"FUNCNAME",
        b"SHELLOPTS", b"BASHOPTS", b"PIPESTATUS",
    ];
    let len = bytes.len();
    let mut i = 0;
    while i < len {
        // Skip single-quoted sections
        if bytes[i] == b'\'' {
            i += 1;
            while i < len && bytes[i] != b'\'' {
                i += 1;
            }
            i += 1;
            continue;
        }
        if bytes[i] == b'$' {
            let start = i + 1;
            for var in BASH_VARS {
                let end = start + var.len();
                if end <= len
                    && bytes[start..end] == **var
                    && (end == len
                        || !bytes[end].is_ascii_alphanumeric() && bytes[end] != b'_')
                {
                    return true;
                }
            }
        }
        i += 1;
    }
    false
}

/// Check for bash brace range expansion like {1..5} or {a..z}.
/// Skips single- and double-quoted sections to avoid false positives.
fn has_brace_range(bytes: &[u8]) -> bool {
    let len = bytes.len();
    let mut i = 0;
    while i < len {
        match bytes[i] {
            b'\'' => {
                i += 1;
                while i < len && bytes[i] != b'\'' {
                    i += 1;
                }
            }
            b'"' => {
                i += 1;
                while i < len && bytes[i] != b'"' {
                    if bytes[i] == b'\\' {
                        i += 1;
                    }
                    i += 1;
                }
            }
            b'{' => {
                let start = i + 1;
                i = start;
                while i < len && bytes[i] != b'}' {
                    i += 1;
                }
                if i < len {
                    let inner = &bytes[start..i];
                    if let Some(dot_pos) = inner.windows(2).position(|w| w == b"..")
                        && dot_pos > 0
                        && dot_pos + 2 < inner.len()
                    {
                        return true;
                    }
                }
            }
            _ => {}
        }
        i += 1;
    }
    false
}

/// Detect bash-only fd redirections: a digit followed by `>` or `<` where the
/// fd number is >= 3. Fish natively supports `0<`, `1>`, and `2>`.
/// Skips single- and double-quoted sections.
fn has_bash_fd_redirect(bytes: &[u8]) -> bool {
    let len = bytes.len();
    let mut i = 0;
    while i < len {
        match bytes[i] {
            b'\'' => {
                i += 1;
                while i < len && bytes[i] != b'\'' { i += 1; }
            }
            b'"' => {
                i += 1;
                while i < len && bytes[i] != b'"' {
                    if bytes[i] == b'\\' { i += 1; }
                    i += 1;
                }
            }
            b'0'..=b'9' => {
                let start = i;
                // Consume all contiguous digits. The `continue` at the end of
                // this arm skips the `i += 1` at the bottom of the outer loop,
                // which is correct because we already advanced `i` past the
                // digit run (and possibly past the redirect operator).
                while i < len && bytes[i].is_ascii_digit() { i += 1; }
                if i < len && matches!(bytes[i], b'>' | b'<') {
                    // Only flag if at a word boundary (not mid-token like "echo 300>f")
                    let is_word_start = start == 0
                        || matches!(bytes[start - 1], b' ' | b'\t' | b';' | b'\n' | b'|' | b'&');
                    if is_word_start {
                        // Fish supports 0<, 1>, 2> natively. Anything >= 3 is bash-only.
                        let num = &bytes[start..i];
                        let is_fish_fd = matches!(num, b"0" | b"1" | b"2");
                        if !is_fish_fd {
                            return true;
                        }
                    }
                }
                continue;
            }
            _ => {}
        }
        i += 1;
    }
    false
}

/// Check if `kw` appears as a standalone word: preceded by a separator
/// (or start of input) and followed by a separator (or end of input).
fn has_word(bytes: &[u8], kw: &[u8]) -> bool {
    let len = bytes.len();
    let kw_len = kw.len();
    let mut i = 0;
    while i + kw_len <= len {
        if bytes[i..i + kw_len] == *kw {
            let pre = i == 0 || matches!(bytes[i - 1], b' ' | b'\t' | b';' | b'\n' | b'|' | b'&');
            let post = i + kw_len == len
                || matches!(bytes[i + kw_len], b' ' | b'\t' | b';' | b'\n' | b'|' | b'&' | b')');
            if pre && post {
                return true;
            }
        }
        i += 1;
    }
    false
}

/// Given `NAME=` at `eq_pos`, skip past the value and check whether a command
/// follows. Returns `Some(pos)` if a token follows (prefix assignment — valid
/// fish 3.1+), or `None` if bare (bash-only).
fn skip_prefix_value(bytes: &[u8], eq_pos: usize) -> Option<usize> {
    let len = bytes.len();
    let mut j = eq_pos + 1;
    // Skip value (handles mixed quoting)
    while j < len && !matches!(bytes[j], b' ' | b'\t' | b'\n' | b';' | b'|' | b'&') {
        match bytes[j] {
            // Skip quoted values; if unterminated, j stays at len and
            // the outer while condition (j < len) exits gracefully.
            b'\'' => {
                j += 1;
                while j < len && bytes[j] != b'\'' { j += 1; }
                if j < len { j += 1; }
            }
            b'"' => {
                j += 1;
                while j < len && bytes[j] != b'"' {
                    if bytes[j] == b'\\' { j += 1; }
                    j += 1;
                }
                if j < len { j += 1; }
            }
            _ => j += 1,
        }
    }
    // Skip whitespace after value
    while j < len && matches!(bytes[j], b' ' | b'\t') { j += 1; }
    // Bare if nothing or a separator follows; otherwise a command is next
    if j >= len || matches!(bytes[j], b'\n' | b';' | b'|' | b'&') {
        None
    } else {
        Some(j)
    }
}

/// Check for bash-specific syntax at command position:
/// `NAME=` (bare), `NAME+=`, `NAME[..]=`, `NAME()`, `(` subshell, or `{` brace group.
/// `NAME=value cmd` (prefix assignment) is valid fish 3.1+ and is NOT flagged.
/// Fish only allows `(cmd)` in argument position. Skips quoted sections.
fn has_bash_cmd_start(bytes: &[u8]) -> bool {
    let len = bytes.len();
    let mut i = 0;
    // 0 = expecting first word (skip whitespace), 1 = inside first word, 2 = past it
    let mut state: u8 = 0;
    while i < len {
        match bytes[i] {
            b'\'' => {
                state = 2;
                i += 1;
                while i < len && bytes[i] != b'\'' {
                    i += 1;
                }
            }
            b'"' => {
                state = 2;
                i += 1;
                while i < len && bytes[i] != b'"' {
                    if bytes[i] == b'\\' {
                        i += 1;
                    }
                    i += 1;
                }
            }
            b';' | b'\n' | b'|' | b'&' => state = 0,
            b' ' | b'\t' if state == 0 => {}
            b' ' | b'\t' => state = 2,
            b'(' if state == 0 => return true, // subshell at command start
            // { at command start with whitespace after = bash brace group
            // (fish brace expansion {a,b,c} has no space after {)
            b'{' if state == 0
                && i + 1 < len
                && matches!(bytes[i + 1], b' ' | b'\t' | b'\n') =>
            {
                return true;
            }
            b'(' if state == 1 => return true, // NAME() — bash function def
            b'=' if state == 1 => match skip_prefix_value(bytes, i) {
                None => return true, // bare NAME=val — bash-only
                Some(next) => { i = next; state = 0; continue; }
            }
            _ if state == 0 => {
                if bytes[i].is_ascii_alphabetic() || bytes[i] == b'_' {
                    state = 1;
                } else {
                    state = 2;
                }
            }
            _ if state == 1 => {
                if bytes[i] == b'+' && i + 1 < len && bytes[i + 1] == b'=' {
                    return true; // NAME+=
                }
                // NAME[...]= or NAME[...]+=  (array element assignment)
                if bytes[i] == b'[' {
                    let mut j = i + 1;
                    while j < len && bytes[j] != b']' {
                        j += 1;
                    }
                    if j + 1 < len && bytes[j + 1] == b'=' {
                        return true;
                    }
                    if j + 2 < len && bytes[j + 1] == b'+' && bytes[j + 2] == b'=' {
                        return true;
                    }
                }
                if !bytes[i].is_ascii_alphanumeric() && bytes[i] != b'_' {
                    state = 2;
                }
            }
            _ => {}
        }
        i += 1;
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn detects_export() {
        assert!(looks_like_bash("export PATH=/usr/bin:$PATH"));
        assert!(looks_like_bash("export EDITOR=vim"));
    }

    #[test]
    fn detects_for_loop() {
        assert!(looks_like_bash("for i in $(seq 5); do echo $i; done"));
    }

    #[test]
    fn detects_if_then() {
        assert!(looks_like_bash("if [ -f foo ]; then echo yes; fi"));
    }

    #[test]
    fn dollar_paren_is_valid_fish() {
        // $() is valid fish 3.4+ command substitution — not bash-specific
        assert!(!looks_like_bash("echo $(whoami)"));
        assert!(!looks_like_bash("set myvar $(string upper hello)"));
        assert!(!looks_like_bash("echo $(date)"));
        // But $(( )) is bash arithmetic — still detected
        assert!(looks_like_bash("echo $((2 + 2))"));
        assert!(looks_like_bash("echo $((1+2))"));
        // $(( )) inside double quotes with apostrophes must still be detected
        assert!(looks_like_bash(r#"echo "Hello $(whoami), it's $((2+2)) o'clock""#));
    }

    #[test]
    fn detects_double_brackets() {
        assert!(looks_like_bash("[[ -n \"$HOME\" ]] && echo yes"));
    }

    #[test]
    fn detects_parameter_expansion() {
        assert!(looks_like_bash("echo ${HOME:-/tmp}"));
    }

    #[test]
    fn detects_standalone_double_paren() {
        assert!(looks_like_bash("(( i++ ))"));
        assert!(looks_like_bash("(( x += 5 ))"));
        assert!(looks_like_bash("(( count = 0 ))"));
        assert!(looks_like_bash("echo $((2 + 2))"));
    }

    #[test]
    fn ignores_plain_fish() {
        assert!(!looks_like_bash("echo hello"));
        assert!(!looks_like_bash("set -gx PATH /usr/bin $PATH"));
        assert!(!looks_like_bash("for i in (seq 5); echo $i; end"));
    }

    #[test]
    fn brace_range_unquoted() {
        assert!(has_brace_range(b"{1..5}"));
        assert!(has_brace_range(b"echo {a..z}"));
        assert!(has_brace_range(b"{1..10..2}"));
        assert!(!has_brace_range(b"{..5}"));
        assert!(!has_brace_range(b"{1..}"));
    }

    #[test]
    fn brace_range_skips_quotes() {
        assert!(!has_brace_range(b"echo '{1..5}'"));
        assert!(!has_brace_range(br#"echo "{1..5}""#));
        assert!(has_brace_range(b"echo '{skip}' {1..5}"));
    }

    #[test]
    fn ignores_fish_and_or_operators() {
        // && and || are valid fish 3.0+ syntax — not bash-specific
        assert!(!looks_like_bash("echo foo && echo bar"));
        assert!(!looks_like_bash("echo foo || echo bar"));
        assert!(!looks_like_bash("true && false || echo fallback"));
    }

    #[test]
    fn detects_bare_assignment() {
        assert!(looks_like_bash("FOO=hello"));
        assert!(looks_like_bash("FOO=hello && echo $FOO"));
        assert!(looks_like_bash("x=1"));
        assert!(looks_like_bash("_VAR=value"));
        assert!(looks_like_bash("echo ok; FOO=bar"));
    }

    #[test]
    fn detects_subshell() {
        assert!(looks_like_bash("(cd /tmp && pwd)"));
        assert!(looks_like_bash("(echo a; echo b) | sort"));
        assert!(looks_like_bash("echo ok; (cd /tmp)"));
    }

    #[test]
    fn subshell_skips_fish_cmd_substitution() {
        // fish (cmd) in argument position — not a subshell
        assert!(!looks_like_bash("for i in (seq 5); echo $i; end"));
        assert!(!looks_like_bash("echo (date)"));
        assert!(!looks_like_bash("set x (pwd)"));
    }

    #[test]
    fn bare_assignment_skips_false_positives() {
        // fish set command — not a bash assignment
        assert!(!looks_like_bash("set -gx PATH /usr/bin"));
        // = inside quotes
        assert!(!looks_like_bash("echo 'FOO=bar'"));
        assert!(!looks_like_bash(r#"echo "FOO=bar""#));
        // Not at token boundary (part of a larger word)
        assert!(!looks_like_bash("echo FOO=bar"));
    }

    #[test]
    fn detects_assignment_after_operators() {
        // Bare assignments after operators — bash-only
        assert!(looks_like_bash("echo ok && FOO=bar"));
        assert!(looks_like_bash("echo ok || FOO=bar"));
        assert!(looks_like_bash("echo ok & FOO=bar"));
        // Prefix assignment before command — valid fish 3.1+
        assert!(!looks_like_bash("echo ok | FOO=bar cat"));
    }

    #[test]
    fn prefix_assignment_is_valid_fish() {
        // NAME=value command is valid fish 3.1+ — not bash-specific
        assert!(!looks_like_bash("FOO=bar echo hello"));
        assert!(!looks_like_bash("GIT_DIR=. git status"));
        assert!(!looks_like_bash("FOO=bar BAZ=qux echo hello"));
        assert!(!looks_like_bash("FOO='hello world' echo test"));
        assert!(!looks_like_bash("FOO= echo hello"));
        // But bare assignments (no command after) ARE bash-only
        assert!(looks_like_bash("FOO=bar"));
        assert!(looks_like_bash("FOO=bar BAZ=qux"));
        assert!(looks_like_bash("A=1 B=2"));
    }

    #[test]
    fn detects_function_definition() {
        assert!(looks_like_bash("greet() { echo hello; }"));
        assert!(looks_like_bash("greet() { echo \"Hello, $1!\"; }; greet \"World\""));
        assert!(looks_like_bash("_my_func() { pwd; }"));
    }

    #[test]
    fn detects_special_variables() {
        assert!(looks_like_bash("echo $#"));
        assert!(looks_like_bash("echo \"args: $#\""));
        assert!(looks_like_bash("echo $?"));
        assert!(looks_like_bash("echo $!"));
        assert!(looks_like_bash("echo $$"));
        assert!(looks_like_bash("echo $0"));
        assert!(looks_like_bash("echo $1"));
        assert!(looks_like_bash("echo $@"));
        assert!(looks_like_bash("echo $*"));
    }

    #[test]
    fn detects_backtick_substitution() {
        assert!(looks_like_bash("echo `hostname`"));
        assert!(looks_like_bash("`whoami`"));
    }

    #[test]
    fn detects_compound_assignment() {
        assert!(looks_like_bash("arr+=(4 5)"));
        assert!(looks_like_bash("str+=hello"));
        assert!(looks_like_bash("echo ok; x+=1"));
    }

    #[test]
    fn detects_array_element_assignment() {
        assert!(looks_like_bash("arr[0]=hello"));
        assert!(looks_like_bash("arr[1]+=more"));
        assert!(looks_like_bash("echo ok; arr[2]=val"));
    }

    #[test]
    fn detects_brace_group() {
        assert!(looks_like_bash("{ echo a; echo b; }"));
        assert!(looks_like_bash("{ echo a; } > /tmp/out"));
        assert!(looks_like_bash("echo ok; { echo a; }"));
    }

    #[test]
    fn brace_group_skips_fish_brace_expansion() {
        // fish brace expansion — no space after {
        assert!(!looks_like_bash("echo {a,b,c}"));
        assert!(!looks_like_bash("mkdir -p /tmp/{x,y,z}"));
    }

    #[test]
    fn detects_ansi_c_quoting() {
        assert!(looks_like_bash("echo $'hello\\nworld'"));
        assert!(looks_like_bash("echo $'\\t'"));
    }

    #[test]
    fn keyword_boundary_avoids_false_positives() {
        // "fi" inside words like "file", "find", "diff"
        assert!(!looks_like_bash("cat file.txt"));
        assert!(!looks_like_bash("diff file1 file2"));
        assert!(!looks_like_bash("find . -name '*.py'"));
        // "then" in normal text (no longer a boundary keyword)
        assert!(!looks_like_bash("echo \"and then\""));
        assert!(!looks_like_bash("echo then we go home"));
        // "done" inside normal text
        assert!(!looks_like_bash("echo \"I am done\""));
        // "let" inside normal text (quoted avoids boundary match)
        assert!(!looks_like_bash("echo \"let me think\""));
        // But real bash keywords still detected
        assert!(looks_like_bash("if true; then echo yes; fi"));
        assert!(looks_like_bash("for i in 1 2; do echo $i; done"));
        assert!(looks_like_bash("let x=5"));
    }

    #[test]
    fn skips_dollar_in_single_quotes() {
        // awk/sed with $1, $2 etc. inside single quotes — NOT bash
        assert!(!looks_like_bash("awk '{print $1}' file"));
        assert!(!looks_like_bash("awk '{print $1, $2}' file.txt"));
        assert!(!looks_like_bash("sed 's/$HOME/foo/'"));
        // But $1 outside quotes IS bash
        assert!(looks_like_bash("echo $1"));
        // $'...' (ANSI-C quoting) should still be detected
        assert!(looks_like_bash("echo $'hello\\nworld'"));
    }

    #[test]
    fn skips_bash_vars_in_single_quotes() {
        assert!(!looks_like_bash("echo '$RANDOM'"));
        assert!(!looks_like_bash("awk '{print $RANDOM}'"));
        // But outside quotes, still detected
        assert!(looks_like_bash("echo $RANDOM"));
    }

    #[test]
    fn skips_commands_with_quoted_dollar() {
        // Common tools with $ inside single quotes — NOT bash
        assert!(!looks_like_bash("sed 's/foo/bar/g' file"));
        assert!(!looks_like_bash("sed -i 's/old/new/g' file.txt"));
        assert!(!looks_like_bash("grep -E 'pattern' file"));
        assert!(!looks_like_bash("grep -r 'TODO' ."));
        assert!(!looks_like_bash("find . -name '*.txt'"));
    }

    #[test]
    fn ignores_fish_builtins() {
        assert!(!looks_like_bash("set -l myvar hello"));
        assert!(!looks_like_bash("set -gx PATH /usr/bin $PATH"));
        assert!(!looks_like_bash("string match -r 'pattern' input"));
        assert!(!looks_like_bash("string replace -a old new $var"));
        assert!(!looks_like_bash("math '2 + 2'"));
    }

    #[test]
    fn ignores_simple_commands() {
        assert!(!looks_like_bash("echo hello world"));
        assert!(!looks_like_bash("ls -la /tmp"));
        assert!(!looks_like_bash("cd /tmp && ls"));
        assert!(!looks_like_bash("mkdir -p /tmp/test"));
    }

    #[test]
    fn detects_heredoc() {
        assert!(looks_like_bash("cat <<'EOF'\nhello\nEOF"));
        assert!(looks_like_bash("cat <<EOF\nhello\nEOF"));
        assert!(looks_like_bash("cat <<-'EOF'\nhello\nEOF"));
    }

    #[test]
    fn detects_bash_only_variables() {
        assert!(looks_like_bash("echo $RANDOM"));
        assert!(looks_like_bash("echo $SECONDS"));
        assert!(looks_like_bash("echo $BASH_VERSION"));
        assert!(looks_like_bash("echo $LINENO"));
        assert!(looks_like_bash("echo $FUNCNAME"));
        assert!(looks_like_bash("echo $PIPESTATUS"));
        // Should not match variable names that start with a bash var name
        assert!(!looks_like_bash("echo $RANDOM_SEED"));
        assert!(!looks_like_bash("echo $SECONDS_ELAPSED"));
    }

    #[test]
    fn detects_fd_redirections() {
        // exec with fd manipulation — bash-only (fd >= 3)
        assert!(looks_like_bash("exec 3>&1 4>&2"));
        assert!(looks_like_bash("exec 3>/dev/null"));
        // Standalone fd >= 3
        assert!(looks_like_bash("echo hello 3>&1"));
        assert!(looks_like_bash("cmd 5>/tmp/log"));
        // Fish natively supports 0<, 1>, 2> — don't flag these
        assert!(!looks_like_bash("echo hello 2>/dev/null"));
        assert!(!looks_like_bash("cmd 2>&1"));
        assert!(!looks_like_bash("cmd 1>/dev/null"));
        assert!(!looks_like_bash("cat 0</dev/stdin"));
        // Digits in other contexts — not fd redirections
        assert!(!looks_like_bash("echo 300"));
        assert!(!looks_like_bash("echo 3 > file"));  // space before > = not fd redirect
        assert!(!looks_like_bash("seq 1 10"));
    }
}