Search code examples
bashcygwin

Error: "The system cannot find the file specified" when using -r flag


Using cygwin bash on Windows XP. Bash version: 4.3.46(7)-release. Minimal working example, sorting an array in reversed order:

#!/bin/bash
array=("a c" b f "3 5")
IFS=$'\r\n' sorted=($(sort -r <<<"${array[*]}"))
printf "[%s]\n" "${sorted[@]}"

Error in cygwin:

-rThe system cannot find the file specified.

On Linux works fine. The error is caused by -r flag. How to fix?


Solution

  • Seems like your version of sort does not support the -r flag. You could ...

    • search in man sort for an equivalent option.
    • Sort normally and reverse the sorted output using another command, for instance tac (the reverse of cat), or if that isn't available perl -e 'print reverse <>'.
      Example: Instead of sort -r <<< "string" use sort <<< "string" | tac.

    Please also have a look at Glenn Jackman's Answer regarding possible problems in your script.