# Ugly hack for Read history on startup #zp4

**URL:** <https://forum.codecrafters.io/t/ugly-hack-for-read-history-on-startup-zp4/11865>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [June 8, 2025, 10:36pm UTC](https://forum.codecrafters.io/t/ugly-hack-for-read-history-on-startup-zp4/11865 "2025-06-08T22:36:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![razvbir](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/razvbir/32/12594_2.png) [@razvbir](https://forum.codecrafters.io/u/razvbir)\
**Post date:** [June 8, 2025, 10:36pm UTC](https://forum.codecrafters.io/t/ugly-hack-for-read-history-on-startup-zp4/11865/1 "2025-06-08T22:36:35Z")

</div>

Hi,

I was stuck on the shell challange at the history file from env variable. Something wierd happens and the first item of readline\_list\_history() is an emptry string.  
To work around it, I wrote this (admittedly ugly) piece of code, but I’m still not sure why the empty string shows up in the first place.  
All code is [here](https://github.com/razvbir/php-shell).  
Any insights would be greatly appreciated!

Thanks,  
Razvan

```auto
        global $history;
        $localHistory = readline_list_history();
        if (isset($localHistory[0]) && isset($history[0])) {
            $localHistory[0] = $history[0];
        }

        $offset = (int) ($this->args[0] ?? 0);
        $prevCommands = array_slice($localHistory, -$offset, null, true);
        foreach ($prevCommands as $index => $prevCommand) {
            $line = sprintf('%5d %s', $index + 1, trim($prevCommand));
            fwrite($this->out, $line . PHP_EOL);
        }

```

---

<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 9, 2025, 1:16am UTC](https://forum.codecrafters.io/t/ugly-hack-for-read-history-on-startup-zp4/11865/3 "2025-06-09T01:16:18Z")

</div>

Hey @razvbir, I added a couple of logs before `readline_callback_read_char` and just inside `afterLine`:

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

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

Looks like `localHistory[0]` is cleared between `readline_callback_read_char` and `afterLine`:

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

Not sure why that’s happening. Might be a PHP/readline issue.

---

<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 14, 2025, 11:03pm UTC](https://forum.codecrafters.io/t/ugly-hack-for-read-history-on-startup-zp4/11865/4 "2025-06-14T23:03:37Z")

</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:** [June 19, 2025, 11:04pm UTC](https://forum.codecrafters.io/t/ugly-hack-for-read-history-on-startup-zp4/11865/5 "2025-06-19T23:04:07Z")

</div>

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