print colors and new lines
Print text in color, and print new lines from a string, which plain echo may not do.
Steps
-
printf 'one\ntwo\n'new lines, the same in every shell
-
echo -e 'one\ntwo'the same with bash's echo
-
printf '\033[31mred\033[0m\n'red text, then back to normal
-
printf '\033[1;32mOK\033[0m\n'bold green
-
echo "$(tput setaf 4)blue$(tput sgr0)"colors by name of the terminal's capability
Notes
- Colors 30 to 37: black, red, green, yellow, blue, magenta, cyan, white; 1; makes them bold.
Commands used
printftput
Each has a page in lore: lore printf opens it.
Related recipes
- add a folder to the pathLet the shell find programs in a folder, for good, and reload the settings without logging out.
- add to the end of a fileAdd lines to a file without replacing it, also to a file only root can write.
- argument list too long"Argument list too long" from rm, cp or mv: too many files for one command line. Hand them over in batches.
- ask for input in a scriptAsk yes or no, read a password without showing it, or wait for an answer with a time limit.