Skip to content

Instantly share code, notes, and snippets.

@visualjeff
Created February 22, 2016 18:24
Show Gist options
  • Save visualjeff/b2c8953525a27ffa1d72 to your computer and use it in GitHub Desktop.
Save visualjeff/b2c8953525a27ffa1d72 to your computer and use it in GitHub Desktop.
Bash Matrix Example
#!/bin/bash
declare -A matrix
num_rows=4
num_columns=5
for ((i=1;i<=num_rows;i++)) do
for ((j=1;j<=num_columns;j++)) do
matrix[$i,$j]=$RANDOM
done
done
f1="%$((${#num_rows}+1))s"
f2=" %9s"
printf "$f1" ''
for ((i=1;i<=num_rows;i++)) do
printf "$f2" $i
done
echo
for ((j=1;j<=num_columns;j++)) do
printf "$f1" $j
for ((i=1;i<=num_rows;i++)) do
printf "$f2" ${matrix[$i,$j]}
done
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment