# Shell: Stuck on Single quotes #NI6

**URL:** <https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [June 17, 2025, 3:31pm UTC](https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270 "2025-06-17T15:31:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rioba-Ian](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rioba-ian/32/12694_2.png) [@Rioba-Ian](https://forum.codecrafters.io/u/Rioba-Ian)\
**Post date:** [June 17, 2025, 3:31pm UTC](https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270/1 "2025-06-17T15:31:29Z")

</div>

In my function for parsing the input, in my current function below, there is no extra space that will be added if we’ve finished taking in the characters with single input, but we see a whitespace on the failing test.

```rust
pub fn parse_input(input: &str) -> Vec<String> {
    let mut args: Vec<String> = Vec::new();
    let mut extracted_quoted_strings = HashSet::new();
    let mut current_arg_buffer = String::new();
    let mut in_single_quote = false;

    let mut chars = input.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == SINGLE_QUOTE {
            if in_single_quote {
                args.push(current_arg_buffer.clone());
                extracted_quoted_strings.insert(current_arg_buffer.clone());
                current_arg_buffer.clear();
                in_single_quote = false;
            } else {
                if !current_arg_buffer.is_empty() {
                    args.push(current_arg_buffer.clone());
                    current_arg_buffer.clear();
                }
                in_single_quote = true;
            }
        } else if ch.is_whitespace() && !in_single_quote {
            if !current_arg_buffer.is_empty() {
                args.push(current_arg_buffer.clone());
                current_arg_buffer.clear();
            }
        } else {
            current_arg_buffer.push(ch);
        }
    }

    if !current_arg_buffer.is_empty() {
        if in_single_quote {
            args.push(current_arg_buffer.clone());
            extracted_quoted_strings.insert(current_arg_buffer.clone());
        } else {
            args.push(current_arg_buffer.clone());
        }
    }

    println!("{:?}", extracted_quoted_strings);

    args
}

```

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/3/3b0a847234e70ff8543301f9cbe69c7927e14c14.jpeg)

My code is on github [GitHub - Rioba-Ian/codecrafters-shell-rust](https://github.com/Rioba-Ian/codecrafters-shell-rust)

Thank you and I appreciate any help or insight I can get

---

<div class="post-metadata">

**Author:** ![senevoldsen](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/senevoldsen/32/6460_2.png) [@senevoldsen](https://forum.codecrafters.io/u/senevoldsen)\
**Post date:** [June 18, 2025, 6:47pm UTC](https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270/4 "2025-06-18T18:47:44Z")

</div>

The test is telling you what is wrong. You are parsing `'hello''example'` as two arguments.

In Bash “arguments” are referred to as tokens. They are separated by “metacharacters”. When you find the ending single quote you think you are done, so you add the text between `'` as its own element to your argument list. This is wrong because we haven’t reached a metacharacter yet (like an unquoted space). Same thing with [double quotes](https://forum.codecrafters.io/t/test-issue-with-backslash-within-single-quotes-le5-for-go/9166/3) .

---

<div class="post-metadata">

**Author:** ![Rioba-Ian](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rioba-ian/32/12694_2.png) [@Rioba-Ian](https://forum.codecrafters.io/u/Rioba-Ian)\
**Post date:** [June 20, 2025, 4:22pm UTC](https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270/6 "2025-06-20T16:22:50Z")

</div>

Thank you for the explanation, I’ve gotten a good understanding and tried to modify my solution but when testing with echo for the Backslash outside quotes on #YT5 I get error that they were not correct. Yet, on running echo with each of the cat “/tmp/foo/f\n86” “/tmp/foo/f\79” “/tmp/foo/f’'55” they output correctly.

```sh
[tester::#YT5] Running tests for Stage #YT5 (Quoting - Backslash outside quotes)
[tester::#YT5] [setup] export PATH=/tmp/apple/banana/raspberry:$PATH
[tester::#YT5] [setup] echo -n "pineapple orange." > "/tmp/foo/f\\n86"
[tester::#YT5] [setup] echo -n "blueberry strawberry." > "/tmp/foo/f\\79"
[tester::#YT5] [setup] echo -n "blueberry pear." > "/tmp/foo/f'\\'55"
[tester::#YT5] Running ./your_program.sh
[your-program] $ echo hello\ \ \ \ \ \ test
[your-program] hello test
[tester::#YT5] ✓ Received expected response
[your-program] $ echo \'\"test world\"\'
[your-program] '"test world"'
[tester::#YT5] ✓ Received expected response
[your-program] $ echo world\nshell
[your-program] worldnshell
[tester::#YT5] ✓ Received expected response
[your-program] $ cat "/tmp/foo/f\n86" "/tmp/foo/f\79" "/tmp/foo/f'\'55"
[your-program] $ 
[tester::#YT5] ^ Line does not match expected value.
[tester::#YT5] Expected: "pineapple orange.blueberry strawberry.blueberry pear."
[tester::#YT5] Received: "$ "
[tester::#YT5] Assertion failed.
[tester::#YT5] Test failed

```

The exact point where my code for parse\_input is: [codecrafters-shell-rust/src/lib.rs at main · Rioba-Ian/codecrafters-shell-rust · GitHub](https://github.com/Rioba-Ian/codecrafters-shell-rust/blob/main/src/lib.rs)

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [June 22, 2025, 12:48pm UTC](https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270/8 "2025-06-22T12:48:01Z")

</div>

Hey @Rioba-Ian, I tried running your code against the previous stages, but it’s actually still not passing the stage **#NI6 (Quoting - Single quotes)**.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/6/66df376f21de2321f48329bd413860d6c3cbdfcc.png)

**Suggestions** :

1. Use [our CLI](https://docs.codecrafters.io/cli/installation) to test against previous stages by running:

```auto
codecrafters test --previous

```

1. Focus on fixing the early stages first, as later stages depend on them.

---

<div class="post-metadata">

**Author:** ![Rioba-Ian](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rioba-ian/32/12694_2.png) [@Rioba-Ian](https://forum.codecrafters.io/u/Rioba-Ian)\
**Post date:** [June 22, 2025, 2:36pm UTC](https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270/10 "2025-06-22T14:36:11Z")

</div>

Thanks for pointing this out, I fixed the issue that made the previous tests to fail. And the latest tests fail

```auto
[tester::#YT5] Running tests for Stage #YT5 (Quoting - Backslash outside quotes)
[tester::#YT5] [setup] export PATH=/tmp/grape/grape/pear:$PATH
[tester::#YT5] [setup] echo -n "pear orange." > "/tmp/quz/f\\n35"
[tester::#YT5] [setup] echo -n "orange grape." > "/tmp/quz/f\\79"
[tester::#YT5] [setup] echo -n "mango pear." > "/tmp/quz/f'\\'63"
[tester::#YT5] Running ./your_program.sh
[your-program] $ echo example\ \ \ \ \ \ shell
[your-program] example shell
[tester::#YT5] ✓ Received expected response
[your-program] $ echo \'\"shell world\"\'
[your-program] '"shell world"'
[tester::#YT5] ✓ Received expected response
[your-program] $ echo world\nhello
[your-program] worldnhello
[tester::#YT5] ✓ Received expected response
[your-program] $ cat "/tmp/quz/f\n35" "/tmp/quz/f\79" "/tmp/quz/f'\'63"
[your-program] $ 
[tester::#YT5] ^ Line does not match expected value.
[tester::#YT5] Expected: "pear orange.orange grape.mango pear."
[tester::#YT5] Received: "$ "
[tester::#YT5] Assertion failed.
[tester::#YT5] Test failed

```

I think for the tests with the cat they fail on #YT5 but seem to pass on previous tests

---

<div class="post-metadata">

**Author:** ![EshaanAgg](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/eshaanagg/32/399_2.png) [@EshaanAgg](https://forum.codecrafters.io/u/EshaanAgg)\
**Post date:** [June 22, 2025, 4:06pm UTC](https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270/12 "2025-06-22T16:06:47Z")

</div>

The tests are failing as the current implementation is not following the escaping rules correctly regarding `\`:

- If with a single-quoted string, then no escaping is needed.
- If within a double-quoted string, then only escaping should be done for particular characters (described in `#GU3`).

Keeping in mind the same, the code needs to be modified to:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/7/7af98e8a98c50c955e12fb481bf33311ef3ebce9.png)

This would help you pass the stage you’re stuck at while ensuring the previous stages aren’t disturbed. Let me know if something is unclear in the same!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [June 27, 2025, 4:26pm UTC](https://forum.codecrafters.io/t/shell-stuck-on-single-quotes-ni6/12270/14 "2025-06-27T16:26:24Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
