Eoin95
September 25, 2026, 9:52pm
1
I completed the base section of the shell and thought I’d run it locally to play about.
With echo I noticed it’s dropping the first letter, input:“hello” → output:“ello”, Type doesn’t work for any of the commands, it is returning “: not found” and exit returns “: command not found”.
All of my submits have been passing.
Any ideas?
sudzp
October 2, 2026, 3:37pm
3
Can you paste your code to check.
Eoin95
October 3, 2026, 6:07pm
4
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
exit := false
reader := bufio.NewReader(os.Stdin)
for exit == false {
fmt.Print("$ ")
command, err := reader.ReadString('\n')
if err != nil {
panic(err.Error())
}
//Clean command
command = command[:len(command)-1]
command, commandArgs, _ := strings.Cut(command, " ")
commandArguments := ArgumentBuilder(commandArgs)
//fmt.Print("debug: ")
//fmt.Println(commandArguments)
switch command {
case "exit":
{
os.Exit(0)
}
case "echo":
{
echoString := ""
for _, arg := range commandArguments {
if echoString == "" {
echoString = arg
} else {
echoString = echoString + " " + arg
}
}
// for i := 0; i < len(commandArguments); i++ {
// fmt.Printf("%s ", commandArguments[i])
// }
fmt.Println(echoString)
}
case "type":
{
fmt.Println(typeFunction(commandArguments[0]))
}
case "pwd":
{
dir, err := os.Getwd()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(dir)
}
case "cd":
{
dirPath := commandArguments[0]
if commandArguments[0] == "~" {
dirPath = os.Getenv("HOME")
}
err := os.Chdir(dirPath)
if err != nil {
fmt.Println("cd: " + commandArguments[0] + ": No such file or directory")
}
}
default:
{
_, err := exec.LookPath(command)
if err != nil {
fmt.Printf("%s: command not found \n", command)
} else {
cmd := exec.Command(command, commandArguments...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmdErr := cmd.Run()
if cmdErr != nil {
fmt.Println("Executable failed with error: " + cmdErr.Error())
}
}
}
}
}
}
func typeFunction(commandName string) string {
switch commandName {
case "echo", "exit", "type", "pwd", "cd":
{
return commandName + " is a shell builtin"
}
default:
{
foundExec, err := exec.LookPath(commandName)
if err == nil {
return commandName + " is " + foundExec
}
return commandName + ": not found"
}
}
}
func ArgumentBuilder(commandArgs string) []string {
// InQuote bool, commandArguments string array, newString string
// foreach rune in command
// if rune is a single quote
// if InQuote is false
// set InQuote true
// if InQuote is true
// set InQuote to false
// if rune is a space
// if InQuote is true
// add to NewString
// if InQuote is false
// if newString is not empty
// add new string to commandArguments
// reset newString
// Default
// Add rune to newString
//
InQuote := false
var commandArguments []string
newString := ""
for _, char := range commandArgs {
switch char {
case '\'':
if !InQuote {
InQuote = true
} else {
InQuote = false
}
case ' ':
if InQuote {
newString = newString + string(char)
} else {
if newString != "" {
commandArguments = append(commandArguments, newString)
newString = ""
}
}
default:
newString = newString + string(char)
}
}
if newString != "" {
commandArguments = append(commandArguments, newString)
}
return commandArguments
}
Here it is, I am up to the double quotes task but haven’t started it.
Here’s a screenshot showing the tests passing
And here’s a screenshot showing it not working when run locally