1

I need to iterate from c to k alphabetically in shellscript, the attempt below failed, how do I do it properly?

for((i='c';i<'k';i++))
do
  echo $i
done
user678070
  • 1,415
  • 3
  • 18
  • 28

3 Answers3

2

This will work in bash at least:

for i in {c..k}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Is there a way to do this in shell (not bash)? – Jakub P Jun 14 '17 at 10:17
  • @JakubP: What is "shell (not bash)"? Korn shell? Zsh? Fish? Dash? – John Zwinck Jun 20 '17 at 03:31
  • I mean shell as sh. it's always a problem to search for shell scripts, as I haven't found any specific name for it such as bash, ksh, zsh - those are unique. And when one types shell, then it's confusing :-) – Jakub P Jun 28 '17 at 18:38
  • @JakubP: There is no "sh." There are implementations like the ones I listed. `sh` is not a specific thing, but rather a cluster of related things. – John Zwinck Jun 29 '17 at 01:52
2

this one works in my shell (Bash 3.0+):

for c in {c..k}
do
  echo $c
done
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
1

Given that you are not asking for bashisms specifically, here are some alternatives without them.

Simple:

for c in c d e f g h i j k; do
    echo $c
done

Avoiding the need to list all the characters:

s=$(printf %d "'c")
e=$(printf %d "'k")
i=$s
while [ $i -le $e ]; do
    c=$(printf \\$(printf %o $i))
    echo $c
    i=$((i+1))
done
jilles
  • 10,509
  • 2
  • 26
  • 39