2

I have sub-folders named 00 01 02, ... n with each foldername containing at least 2 digits. I need to access these sub-folders in os-x terminal. The command

for i in $(seq -w 00 06); do
echo $i;
done

produces an output of 0 1 2 3 4 5 6. However, the above code in RHEL will produce an output of 00 01 02 03 04 05 06. How can I enforce mac terminal to produce an output of at least 2 digits similar to RHEL?

2

I don't have an os-x to try it on, but perhaps this alternative works:

seq -f '%02.0f' 0 6
0

OS/X comes with zsh, so you can write your script in zsh instead (note that you were already using zsh syntax (since your $i was not quoted which invokes the split+glob operator in bash):

for x in {00..06}; do
  echo $x
done

Posixly, you can always do:

x=0; while [ "$x" -le 6 ]; do
  printf '%02d\n' "$x"
  x=$((x + 1))
done

Or

seq() (first=$1 last=$2 width=${3:-0} step=${4:-1}
  awk "BEGIN{for (i = $first; i <= $last; i += $step)
    printf \"%0${width}d\n\", i}"
)

unset -v IFS # make sure we get the default separator
             # for the split+glob invocation below
for i in $(seq 0 6 2); do
  echo "$i"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.