Revision 4d515d2a-a93c-4c3b-afcd-b2e140d1cb55 - Code Golf Stack Exchange

# [Bash], <s>73</s> <s>68</s> 66 bytes

<!-- language-all: lang-bash -->

 IFS=
 [[ 1ドル != olleh ]]&&read -rN1 c&&echo -n $c&&exec 0ドル $c${1::4}

Assumes a directory with no or only hidden files. Must be run as `<path/to/script>`.

[Try it online!]

### How it works (outdated)

At the beginning of the *while* loop, we first test if the string in the variable **s** (initially empty) equals **olleh** (**hello** backwards, olé), and return **0** (match) or **1** (not a match) accordingly. While formally part of the loop's condition, the outcome won't affect it on its own, as only the last command before `do` determines if the condition holds.

Next, we set the internal field separator to the empty string (so `read` won't choke on whitespace), read raw bytes (`-r`) from STDIN and store them in `c`. `$?` is the exit code of the previous command, so this reads exactly one (`-N1`) byte for a non-match and zero bytes (`-N0`). Reading zero bytes, whether its due to hitting EOF or because `-N0` was specified, causes `read` to exit with status code **1**, so the while loop will end; otherwise, the body is executed and we start over.

In the body, we first print the byte we read, then update **s** with `s=$c${s::4}`. This prepends the read byte to (up to) the first four bytes in **s**, so **s** will equal **olleh** once **hello** has been printed.

[Bash]: https://www.gnu.org/software/bash/
[Try it online!]: https://tio.run/nexus/bash#Lc0xC8IwEAXgOfcrnhDSQRoNOJXWUeji4lg6hEsgQkykOhTE315TdTk@Ho93bJ84QgcfY0bbotLV0p8uHQ0DpMGmQ47RB4yjUpO3DvV0NmClPIeMOkGunj1D7ovlyzTN4b1oIg637LCd/@Okdz8sPYmc2JMQwbpyhV2Zp0eJ2JZvjr7F@zVZ/QE "Bash – TIO Nexus"

AltStyle によって変換されたページ (->オリジナル) /