Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 3 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Bash, (削除) 73 (削除ここまで) (削除) 68 (削除ここまで) 66 bytes

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/<script name>to/script>.

Try it online! 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, (削除) 73 (削除ここまで) (削除) 68 (削除ここまで) 66 bytes

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

Must be run as ./<script name>.

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, (削除) 73 (削除ここまで) (削除) 68 (削除ここまで) 66 bytes

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.

added 50 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Bash, (削除) 73 (削除ここまで) 68(削除) 68 (削除ここまで) 66 bytes

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

Try it online! Must be run as ./<script name>.

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, (削除) 73 (削除ここまで) 68 bytes

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

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, (削除) 73 (削除ここまで) (削除) 68 (削除ここまで) 66 bytes

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

Must be run as ./<script name>.

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.

added 1 character in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Bash, 73(削除) 73 (削除ここまで) 68 bytes

while IFS=
[[ $s$* != olleh ]]
IFS= read]]&&read -rN$? c
dorN1 echoc&&echo -n "$c"
s=$c$"$c"&&exec 0ドル $c${s1::4}
done

Try it online! 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, 73 bytes

while [[ $s = olleh ]]
IFS= read -rN$? c
do echo -n "$c"
s=$c${s::4}
done

Try it online!

How it works

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, (削除) 73 (削除ここまで) 68 bytes

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

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.

added 1106 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading

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