namerefs (introduced in bash 4.0) act as aliases for other variables
var=meow
declare -n ref=var
echo $ref # prints meow
ref=moo
echo $var # prints moo
namerefs (introduced in bash 4.0) act as aliases for other variables
var=meow
declare -n ref=var
echo $ref # prints meow
ref=moo
echo $var # prints moo
#if 0 | |
#define _XOPEN_SOURCE 700 | |
#include <aio.h> | |
#include <arpa/inet.h> | |
#include <assert.h> | |
#include <_Complex.h> | |
#include <ctype.h> | |
#include <dirent.h> | |
#include <dlfcn.h> | |
#include <errno.h> |
set -f | |
IFS= | |
rpn () { | |
for token do | |
case $token in | |
+|-|\*|/|%|\*\*) (( stack[-2] = stack[-2] $token stack[-1] )); unset stack[-1] ;; | |
=) (( ${stack[-2]} = stack[-1] )); unset stack[-1] stack[-1] ;; | |
*) stack+=($token) ;; | |
esac |
#include <arpa/inet.h> | |
#include <errno.h> | |
#include <netinet/in.h> | |
#include <pty.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/mman.h> | |
#include <sys/socket.h> | |
#include <sys/wait.h> |
#!/bin/bash | |
pi=205667 | |
scale=65536 | |
maskf0=-$scale | |
mask0f=$((scale-1)) | |
shift=16 | |
pisq=42389628127 | |
pi2=411775 | |
pi_2=102944 | |
pi_4=51472 |
for the latest chapter of what's becoming a blog on the most cursed bash you can imagine, let's do some maths together
euclid's algorithm for gcd could be written like this in python:
>>> def gcd(a, b):
... if b:
... return gcd(b, a%b)
... return a
{n=$1;r=n":";for(p=2;p*p<=n;p++)while(n%p==0){r=r" "p;n/=p}$1=n>1?r" "n:r}1 |
#!/bin/bash | |
printf %b%.b \ | |
'\e7' 'save cursor position' \ | |
'\e[9999;9999H' 'move to bottom right' \ | |
'\e[6n' 'get cursor position' \ | |
'\e8' 'restore cursor' \ | |
'\e[?u' 'kitty kbd proto' \ | |
'\e[?2026$p' 'synchronised output' \ | |
'\e[m' 'reset colours' \ |
#!/bin/bash | |
a=({1..1000000}) | |
TIMEFORMAT=%R | |
for mult in {1..100}; do | |
printf "%s " "$mult" | |
RANDOM=0 | |
time for i in {1..100000}; do | |
: "${a[RANDOM*mult/10]}" | |
done |
#!/bin/bash | |
# max multiple of pi so our maths always fits in a signed 64 bit int | |
pi=314159 | |
scale=100000 | |
# bhaskara's formula | |
# https://en.wikipedia.org/wiki/Bh%C4%81skara_I's_sine_approximation_formula |