# Unable to Solve Problem of Shell - Single Quotes #NI6 in Go

**URL:** https://forum.codecrafters.io/t/unable-to-solve-problem-of-shell-single-quotes-ni6-in-go/4885
**Category:** Challenges
**Tags:** challenge:shell
**Created:** [February 15, 2025, 2:55pm UTC](https://forum.codecrafters.io/t/unable-to-solve-problem-of-shell-single-quotes-ni6-in-go/4885 "2025-02-15T14:55:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ManishBadgotra](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/manishbadgotra/32/4999_2.png) [@ManishBadgotra](https://forum.codecrafters.io/u/ManishBadgotra)
#### Post date: [February 15, 2025, 2:55pm UTC](https://forum.codecrafters.io/t/unable-to-solve-problem-of-shell-single-quotes-ni6-in-go/4885/1 "2025-02-15T14:55:05Z")

</div>

I’m stuck on Stage #NI6.

I’ve tried splitting my input, removing space one by one but yet unable to do process what can be a solution.

Why am i getting Quotes in user have not mentioned any single quote already?

Here are my logs:

```auto
remote: [your-program] $ echo 'hello test'
remote: [your-program] hello test
remote: [tester::#NI6] ✓ Received expected response
remote: [your-program] $ echo test shell
remote: [your-program] test shell
remote: [tester::#NI6] Output does not match expected value.
remote: [tester::#NI6] Expected: "test shell"
remote: [tester::#NI6] Received: "test shell"
remote: [your-program] $
remote: [tester::#NI6] Assertion failed.
remote: [tester::#NI6] Test failed

```

And here’s a snippet of my code:

```go
package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	for {
		fmt.Fprint(os.Stdout, "$ ")
		in, err := bufio.NewReader(os.Stdin).ReadString('\n')
		if err != nil {
			fmt.Println(err.Error())
		}
		in = strings.TrimRight(in, "\n")
		cmds := strings.Split(in, " ")
		cmds = RemoveSingleQuote(cmds)
		switch cmds[0] {
		case "exit":
			os.Exit(0)
		// case "echo":
		// cmdsAltered := RemoveSingleQuote(cmds[1:])
		// fmt.Println(cmdsAltered)
		// // cmds[1] = cmdsAltered
		// // fmt.Println(strings.Join(cmds[1:], " "))
		case "type":
			switch cmds[1] {
			case "exit", "echo", "type", "pwd", "cd", "cat":
				fmt.Printf("%s is a shell builtin\n", cmds[1])
			default:
				paths := strings.Split(os.Getenv("PATH"), ":")
				isFound := false
				for _, path := range paths {
					fullPath := filepath.Join(path, cmds[1])
					if _, err := os.Stat(fullPath); !os.IsNotExist(err) {
						fmt.Printf("%s is %s\n", cmds[1], fullPath)
						isFound = true
						break
					}
				}
				if !isFound {
					fmt.Printf("%s: not found\n", cmds[1])
				}
			}
		case "pwd":
			dir, err := os.Getwd()
			if err != nil {
				fmt.Println("found error in printing directory:" + err.Error())
			}
			fmt.Println(dir)
		case "cd":
			if cmds[1] == "~" {
				if err := os.Chdir(os.Getenv("HOME")); err != nil {
					fmt.Fprintln(os.Stdout, "cd: HOME: No such file or directory")
				}
			} else if err := os.Chdir(cmds[1]); err != nil {
				fmt.Fprintf(os.Stdout, "cd: %s: No such file or directory\n", cmds[1])
			}
		default:
			command := exec.Command(cmds[0], cmds[1:]...)
			command.Stderr = os.Stderr
			command.Stdout = os.Stdout
			if err := command.Run(); err != nil {
				fmt.Printf("%s: command not found\n", cmds[0])
			}
		}
	}
}

func RemoveSingleQuote(args []string) []string {
	s := RemoveExtraSpace(args)
	for i, s := range args {
		args[i] = strings.ReplaceAll(s, "'", "")
	}

	// return strings.Join(s, " ")
	return s
}

func RemoveExtraSpace(args []string) []string {
	var isFirstSpaceAfterLetter = true
	var space = " "
	for i, arg := range args {
		var ans string
		for _, l := range arg {
			a := string(l)
			if a == space && isFirstSpaceAfterLetter {
				// s[i] = l
				isFirstSpaceAfterLetter = false
				continue
			} else if a != space {
				isFirstSpaceAfterLetter = true
			}
			ans = fmt.Sprintf("%s%s", ans, a)
		}
		args[i] = ans
	}
	return args
}

```

---

<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: [February 16, 2025, 6:29pm UTC](https://forum.codecrafters.io/t/unable-to-solve-problem-of-shell-single-quotes-ni6-in-go/4885/3 "2025-02-16T18:29:16Z")

</div>

I’ll take a look and get back to you early next week.

---

<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: [February 18, 2025, 12:32am UTC](https://forum.codecrafters.io/t/unable-to-solve-problem-of-shell-single-quotes-ni6-in-go/4885/4 "2025-02-18T00:32:44Z")

</div>

Hi @ManishBadgotra, the issue stems from here:

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

The `strings.Split` function is incapable of handling multiple spaces, because it breaks on every space character and creates **multiple empty strings** in the resulting slice.

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

Let me know if you’d like further clarification.

---

<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: [March 10, 2025, 9:43am UTC](https://forum.codecrafters.io/t/unable-to-solve-problem-of-shell-single-quotes-ni6-in-go/4885/8 "2025-03-10T09:43:04Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<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: [March 15, 2025, 9:43am UTC](https://forum.codecrafters.io/t/unable-to-solve-problem-of-shell-single-quotes-ni6-in-go/4885/9 "2025-03-15T09:43:19Z")

</div>

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