You can use these ANSI escape codes:
Black 0;30 Dark Gray 1;30 Red 0;31 Light Red 1;31 Green 0;32 Light Green 1;32 Brown/Orange 0;33 Yellow 1;33 Blue 0;34 Light Blue 1;34 Purple 0;35 Light Purple 1;35 Cyan 0;36 Light Cyan 1;36 Light Gray 0;37 White 1;37
And then use them like this in your script:
# .---------- constant part! # vvvv vvvv-- the code from above RED='\033[0;31m' NC='\033[0m' # No Color printf "I ${RED}love${NC} Stack Overflow\n"
which prints love
in red.
From @james-lim’s comment, if you are using the echo
command, be sure to use the -e flag to allow backslash escapes.
# Continued from above example echo -e "I ${RED}love${NC} Stack Overflow"
(don’t add "\n"
when using echo
unless you want to add an additional empty line)
Related Posts:
- How to create a file in Linux from terminal window? [closed]
- How do I parse command line arguments in Bash?
- Multi-line string with extra space (preserved indentation)
- Multi-line string with extra space (preserved indentation)
- Echo newline in Bash prints literal \n
- Echo newline in Bash prints literal \n
- How to reload .bash_profile from the command line?
- What is the purpose of “&&” in a shell command?
- Cannot use mkdir in home directory: permission denied (Linux Lubuntu)
- How can I kill a process by name instead of PID, on Linux?
- What is cp: cannot stat error in Unix, I get this error when trying to copy thing from one folder to another
- What is cp: cannot stat error in Unix, I get this error when trying to copy thing from one folder to another
- How to count lines in a document?
- Copy multiple files from one directory to another from Linux shell
- Linux Bash: Move multiple different files into same directory
- Timeout a command in bash without unnecessary delay
- How do I deal with a filename that starts with the hyphen (-) character?
- Bash: No such file or directory?
- How do I pause my shell script for a second before continuing?
- Cp: target is not a directory
- How do I tell if a regular file does not exist in Bash?
- In the shell, what does ” 2>&1 ” mean?
- What is the preferred Bash shebang?
- Read a file line by line assigning the value to a variable
- How to compare strings in Bash
- Why do you need to put #!/bin/bash at the beginning of a script file?
- “Couldn’t find a file descriptor referring to the console” on Ubuntu bash on Windows
- How to decode Seagate’s hard drive date code in a Bash script
- What does export PS1=”\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ ” mean in MacOS’ bash Terminal?
- In the shell, what does ” 2>&1 ” mean?
- How do I use a regex in a shell script?
- How to grep for case insensitive string in a file?
- Difference between wait and sleep
- How do I execute a bash script in Terminal?
- gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now
- How to make “if not true condition”?
- ‘\r’: command not found – .bashrc / .bash_profile [duplicate]
- Bash script and /bin/bash^M: bad interpreter: No such file or directory
- -bash: syntax error near unexpected token `newline’
- An “and” operator for an “if” statement in Bash
- ‘\r’: command not found – .bashrc / .bash_profile [duplicate]
- How can I check if a directory exists in a Bash shell script?
- Bash script and /bin/bash^M: bad interpreter: No such file or directory [duplicate]
- Passing parameters to a Bash function
- Pass a password to ssh in pure bash
- How can I use “:” as an AWK field separator?
- -bash: syntax error near unexpected token `newline’ for display command
- An “and” operator for an “if” statement in Bash
- How do I use grep to search the current directory for all files having the a string “hello” yet display only .h and .cc files?
- “while :” vs. “while true” [duplicate]
- Echo newline in Bash prints literal \n
- “sed” command in bash
- How can I declare and use Boolean variables in a shell script?
- How to reload .bashrc settings without logging out and back in again?
- How can I declare and use Boolean variables in a shell script?
- What is the meaning of 2 in 2> /dev/null
- Replace one substring for another string in shell script
- Parsing JSON with Unix tools
- Explaining the ‘find -mtime’ command
- How does “cat << EOF" work in bash?
- Bash scripting missing ‘]’
- Pseudo-terminal will not be allocated because stdin is not a terminal
- How do I compare two string variables in an ‘if’ statement in Bash?
- Using find command in bash script
- Return value in a Bash function
- Is bash a programming language?
- Emulating a do-while loop in Bash
- How can I count all the lines of code in a directory recursively?
- Extract file basename without path and extension in bash
- Pass variable from one Jenkins stage to others in sh
- Given two directory trees, how can I find out which files differ by content?
- mv: cannot stat error : No such file or directory error
- Which characters need to be escaped when using Bash?
- Recursively find all files that match a certain pattern
- Diff command along with Grep gives “Binary file (standard input) matches”
- Delete all local git branches
- How can I get the source directory of a Bash script from within the script itself?
- cp: missing destination file operand after
- BASH Syntax error near unexpected token ‘done’
- Shell: How to call one shell script from another shell script?
- bash assign default value
- What does -z mean in Bash?
- How can I match a string with a regex in Bash?
- Pseudo-terminal will not be allocated because stdin is not a terminal
- How to use gdb with pipes and stdin
- find: missing argument to -exec
- echo that outputs to stderr
- How do I set a variable to the output of a command in Bash?
- How to define hash tables in Bash?
- How to escape single quotes within single quoted strings
- Grep ‘binary file matches’. How to get normal grep output?
- ./configure : /bin/sh^M : bad interpreter
- standard_init_linux.go:178: exec user process caused “exec format error”
- Trying to use bash on Windows and got no installed distributions message
- Simple logical operators in Bash
- Bash if statement with multiple conditions throws an error
- How to check if an environment variable exists and get its value?
- What is the difference between “#!/usr/bin/env bash” and “#!/usr/bin/bash”?
- Laravel PHP Command Not Found
- Getting an “ambiguous redirect” error