Rotate

str_rotate

Circular-shift a string by N character positions. Positive N shifts right (tail wraps to front); negative N shifts left. A shift of 0 or a magnitude equal to string length is a no-op. Note: rot13 is a specific case of str_rotate with N=13 over the alphabet; str_rotate works on the full string character positions.

Example

str_rotate "abcdefg"  2    # => "fgabcde"
str_rotate "abcdefg" -2    # => "cdefgab"
str_rotate "abcdefg"  7    # => "abcdefg"

Arguments

  • $1 (string): String to rotate
  • $2 (int): Number of positions to shift (default: 0; negative = left shift)

Exit codes

  • 0: Always; 1 Non-integer shift value

Output on stdout

  • Rotated string