Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Created August 7, 2012 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ormaaj/3284124 to your computer and use it in GitHub Desktop.
Save ormaaj/3284124 to your computer and use it in GitHub Desktop.
environment assignment
#!/usr/bin/env bash
unset -v x y sh
while IFS= read -r s; do
IFS= declare -a "shells=( $s )"
IFS= read -r testCase
printf '%s\n%6s%s\n' "$testCase" '' 'x y x y'
for sh in "${shells[@]}"; do
printf '%-4s: %s\n' "$sh" "$("$sh" -c "$testCase")"
done
echo
done <<"EOF"
sh {{b,d}a,po,{,m}k,z}sh bb
x= y=${x:=1} sh -c 'printf "%1s %1s" "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
sh {{b,d}a,po,{,m}k,z}sh bb
x=2 y=2; x= y=${x:=1} sh -c 'printf "%1s %1s" "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
sh {{b,d}a,po,{,m}k,z}sh bb
y=${x:=1} sh -c 'printf "%1s %1s" "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
sh {{b,d}a,po,{,m}k,z}sh bb
x=2 y=2; x=1 y=$x sh -c 'printf "%1s %1s" "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
sh {{b,d}a,po,{,m}k,z}sh bb
x= y=${x:=1} eval 'printf %1s\ %1s "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
sh {{b,d}a,po,{,m}k,z}sh bb
[ -n "${ZSH_VERSION+_}" ] && setopt -o POSIX_BUILTINS; f() { printf '%1s %1s' "$x" "$y"; }; x=2 y=2; x=1 y= command eval 'y=$x f'; printf ' %1s %1s' "$x" "$y"
{{,m}k,z}sh
[ -n "${ZSH_VERSION+_}" ] && setopt -o POSIX_BUILTINS; function f { printf '%1s %1s' "$x" "$y"; }; x=2 y=2; x=1 y= command eval 'y=$x f'; printf ' %1s %1s' "$x" "$y"
{,m}ksh
function f { printf '%1s %1s' "$x" "$y"; }; x=2 y=2; x= y=${x:=1} f; printf ' %1s %1s' "$x" "$y"
{,m}ksh
f() { printf '%1s %1s' "$x" "$y"; }; x=2 y=2; x= y=${x:=1} f; printf ' %1s %1s' "$x" "$y"
EOF
# vim: set fenc=utf-8 ff=unix ts=4 sts=4 sw=4 ft=sh nowrap et:
x= y=${x:=1} sh -c 'printf "%1s %1s" "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
x y x y
sh : 1 1 1
bash: 1 1 1
dash: 1
posh: 1 1
ksh : 1 1
mksh: 1 1
zsh : 1 1
bb : 1
x=2 y=2; x= y=${x:=1} sh -c 'printf "%1s %1s" "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
x y x y
sh : 1 1 1 2
bash: 1 1 1 2
dash: 1 2 2
posh: 1 1 2 2
ksh : 1 1 2 2
mksh: 2 2 2
zsh : 1 1 2 2
bb : 1 2
y=${x:=1} sh -c 'printf "%1s %1s" "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
x y x y
sh : 1 1
bash: 1 1
dash: 1 1
posh: 1 1
ksh : 1 1
mksh: 1 1
zsh : 1
bb : 1 1
x=2 y=2; x=1 y=$x sh -c 'printf "%1s %1s" "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
x y x y
sh : 1 1 2 2
bash: 1 1 2 2
dash: 1 1 2 2
posh: 1 1 2 2
ksh : 1 1 2 2
mksh: 1 2 2 2
zsh : 1 1 2 2
bb : 1 1 2 2
x= y=${x:=1} eval 'printf %1s\ %1s "$x" "$y"'; printf ' %1s %1s' "$x" "$y"
x y x y
sh : 1 1 1 1
bash: 1 1 1
dash: 1 1 1 1
posh: 1 1 1 1
ksh : 1 1 1 1
mksh: 1 1 1 1
zsh : 1 1
bb :
[ -n "${ZSH_VERSION+_}" ] && setopt -o POSIX_BUILTINS; f() { printf '%1s %1s' "$x" "$y"; }; x=2 y=2; x=1 y= command eval 'y=$x f'; printf ' %1s %1s' "$x" "$y"
x y x y
sh : 1 1 2 2
bash: 1 1 2 2
dash: 1 1 2 2
posh: 1 1 2 2
ksh : 1 1 2 2
mksh: 1 1 2 2
zsh : 1 1 1 1
bb : 2 2 2 2
[ -n "${ZSH_VERSION+_}" ] && setopt -o POSIX_BUILTINS; function f { printf '%1s %1s' "$x" "$y"; }; x=2 y=2; x=1 y= command eval 'y=$x f'; printf ' %1s %1s' "$x" "$y"
x y x y
ksh : 1 1 2 2
mksh: 1 1 2 2
zsh : 1 1 1 1
function f { printf '%1s %1s' "$x" "$y"; }; x=2 y=2; x= y=${x:=1} f; printf ' %1s %1s' "$x" "$y"
x y x y
ksh : 1 1 2 2
mksh: 2 2 2
f() { printf '%1s %1s' "$x" "$y"; }; x=2 y=2; x= y=${x:=1} f; printf ' %1s %1s' "$x" "$y"
x y x y
ksh : 1 1 1 1
mksh: 1 1 1 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment