Use expansion globs {a..z..2} in zsh like in bash

2 min read 21-10-2024
Use expansion globs {a..z..2} in zsh like in bash

When it comes to shell scripting, both Bash and Zsh are popular choices among developers. One of the powerful features available in Bash is the use of expansion globs, specifically the notation {a..z..2}, which generates a sequence of characters. However, Zsh users may not be familiar with how to implement similar functionality. In this article, we'll clarify how to use expansion globs in Zsh and provide practical examples to enhance your scripting skills.

Understanding the Problem Scenario

Original Problem Code:

echo {a..z..2}

This line of code in Bash generates a sequence of letters from a to z with a step of 2, resulting in a c e g i k m o q s u w y. Zsh users may find that this notation does not work in the same way as it does in Bash.

How to Use Expansion Globs in Zsh

While Zsh has its own powerful features, it does not directly support the same syntax for generating sequences as Bash does. However, you can achieve similar results by utilizing the seq command or Zsh’s built-in functionalities.

Here are a couple of approaches to achieve character expansion in Zsh:

1. Using seq Command

You can use the seq command combined with the tr command to generate a sequence of letters:

seq -f "%g" 97 122 | tr -d '\n' | sed 's/./& /g' | awk '{for(i=1;i<=NF;i+=2) printf $i}'
  • Explanation:
    • seq -f "%g" 97 122 generates numbers from 97 (ASCII for a) to 122 (ASCII for z).
    • tr -d '\n' transforms it into a single line.
    • sed 's/./& /g' adds a space after every character.
    • awk '{for(i=1;i<=NF;i+=2) printf $i}' prints every second character, effectively giving you the desired output.

2. Using Zsh's Brace Expansion

Zsh supports brace expansion but lacks the step functionality directly. However, you can manually create groups of letters:

echo {a,c,e,g,i,k,m,o,q,s,u,w,y}
  • Explanation:
    • This method requires you to define each character you want to output, making it less dynamic but fully compatible with Zsh.

Practical Examples

Let’s illustrate how to use these methods to generate sequences in Zsh:

Example 1: Generating a Custom Sequence

for char in {a..z}; do
  echo -n "$char "
done | awk '{for(i=1;i<=NF;i+=2) printf $i " "}'

This will output every second letter from a to z in Zsh.

Example 2: Creating a Loop with Characters

for char in {a,c,e,g,i}; do
  echo "$char"
done

This loop will print the characters a, c, e, g, i, each on a new line.

Conclusion

While Zsh does not directly support the expansion glob syntax {a..z..2} like Bash, there are workarounds using the seq command and other Zsh features. Understanding these differences is crucial for efficient shell scripting. By employing the methods discussed, Zsh users can still generate and manipulate character sequences effectively.

Useful Resources

By familiarizing yourself with these tips and tools, you can enhance your scripting abilities in Zsh and make the most out of its features!