3

I would like to pass colors to an echo command executed after find results:

For example this works:

RED='\033[0;31m'
echo -e "${RED}Red"

But this one does not work:

RED='\033[0;31m'
find . -print -name test -exec bash -c 'echo -e "${RED}{}"' \;

Is this not possible in this way, or is another option like using -printf better? My goal would be to see find output (with -print) and highlight in another color executed find results! Thanks for help or input!

Madhias
  • 33
  • 3
  • [Difference between single and double quotes in Bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Nov 26 '16 at 08:23

1 Answers1

0

You will need to export RED variable since -exec bash -c spawns a new subshell and RED isn't available to child shell unless it is exported:

export RED='\033[0;31m'

Also find command should be:

find . -name test -exec bash -c 'echo -e "${RED}{}"' \;

You have an extra -print that will print all entries not just named test.

Prefer this form of passing filename to bash -c in argument list:

find . -name 'test' -exec bash -c 'echo -e "$RED$1"' - '{}' \;
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Thanks, that did the trick! I am new to bash as you see... I do want to see the verbose output from find, therefor I set the -print option. – Madhias Nov 26 '16 at 17:47