Avatar of tivasyk

tivasyk's solution

to Reverse String in the Bash Track

Published at Dec 09 2018 · 0 comments
Test suite


This exercise has changed since this solution was written.

Reverse a string

For example: input: "cool" output: "looc"

Run the tests with:

bats reverse_string_test.sh

After the first test(s) pass, continue by commenting out or removing the skip annotations prepending other tests.


Introductory challenge to reverse an input string https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

External utilities

Bash is a language to write scripts that works closely with various system utilities, like sed, awk, date and even other programming languages, like Python. This track does not restrict the usage of these utilities, and as long as your solution is portable between systems and does not require installing third party applications, feel free to use them to solve the exercise.

For an extra challenge, if you would like to have a better understanding of the language, try to re-implement the solution in pure Bash, without using any external tools.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


#!/usr/bin/env bash

@test "An empty string" {
  run bash reverse_string.sh ""

  [ "$status" -eq 0 ]
  [ "$output" = "" ]

@test "A word" {
  run bash reverse_string.sh "robot"

  [ "$status" -eq 0 ]
  [ "$output" = "tobor" ]

@test "A capitalised word" {
  run bash reverse_string.sh "Ramen"

  [ "$status" -eq 0 ]
  [ "$output" = "nemaR" ]

@test "A sentence with punctuation" {
  run bash reverse_string.sh "I'm hungry!"

  [ "$status" -eq 0 ]
  [ "$output" = "!yrgnuh m'I" ]

@test "A palindrome" {
  run bash reverse_string.sh "racecar"

  [ "$status" -eq 0 ]
  [ "$output" = "racecar" ]
#!/usr/bin/env bash
# tivasyk <tivasyk@gmail.com>

main() {

    # Read the input string from end to start into output
    for (( i=$(( ${#input}-1 )); i>=0; i-- )); do
    echo "${output}"
main "$@"

Community comments

Find this solution interesting? Ask the author a question to learn more.

tivasyk's Reflection

i think i am beginning to love exercism despine tons of crap in "community solutions" =)