Should we introduce a test about the drawbacks of echo?

In the “Why is printf better than echo?” vein, we could add a bash-specific test to reverse-string like

  run bash reverse_string.sh 'n-'
  assert_success
  assert_output '-n'

Then any solution that does echo "$reversed_string" will fail.

Is this cruel?

Not if it saves some foot-gunning on the part of the student in the future. Better to struggle with it in an exercise than get bitten by it in a program or script.

You could also add a hint to get anyone who is frustrated unstuck.

2 Likes

Possibly :smiley:

But it also teaches a pretty fun/interesting/frustrating aspect of echo. On the one hand, that test teaches a pretty important lesson. On the other hand, my initial reaction includes some concern about how many students will get absolutely stuck here and be unable to solve this otherwise-simple(ish) exercise.

Would that concern be alleviated by hints or appended instructions?

If only…

echo () {
    local fmt='%s' newline='\n'
    local IFS=' ' OPTIND OPTARG

    while getopts :neE opt; do
        case $opt in
            n)  newline='' ;;
            e)  fmt='%b' ;;
            E)  fmt='%s' ;;
            *)  builtin printf '%s\n' "usage: $0 [-neE] [--] [string ...]" >&2
                return 1
                ;;
        esac
    done
    shift $((OPTIND - 1))

    # shellcheck disable=SC2059
    builtin printf "${fmt}${newline}" "$*"
}

then

$ echo -n         # no output, as per bash
$ echo -- -n
-n

If we can provide the hint for when the tests fails for that reason, I think that is the best way to communicate the hint.

3 Likes

Yeah. A hint and/or append would alleviate much of my concern there.