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
- Bash: No such file or directory?
- How do I tell if a regular file does not exist in Bash?
- Read a file line by line assigning the value to a variable
- Why do you need to put #!/bin/bash at the beginning of a script file?
- 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?
- -bash: syntax error near unexpected token `newline’
- Pass a password to ssh in pure bash
- -bash: syntax error near unexpected token `newline’ for display command
- 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?
- How to reload .bashrc settings without logging out and back in again?
- How can I declare and use Boolean variables in a shell script?
- Replace one substring for another string in shell script
- How does “cat << EOF" work in bash?
- 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
- Is bash a programming language?
- Emulating a do-while loop in Bash
- Pass variable from one Jenkins stage to others in sh
- 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
- bash assign default value
- What does -z mean in Bash?
- find: missing argument to -exec
- How do I set a variable to the output of a command in Bash?
- Grep ‘binary file matches’. How to get normal grep output?
- Trying to use bash on Windows and got no installed distributions message
- 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
- Bash syntax error: unexpected end of file
- Is there a “goto” statement in bash?
- Difference between return and exit in Bash functions
- How to modify a global variable within a function in bash?
- Bash script: bad interpreter
- choosing between $0 and BASH_SOURCE
- How do I automatically restart a Minecraft Spigot server in the event of a crash or /stop when using screen?
- Create a new file in git bash
- Fast ping sweep in python
- Using SED with wildcard
- chmod: changing permissions of ‘my_script.sh’: Operation not permitted
- How can I check if a program exists from a Bash script?
- Bash command to sum a column of numbers
- Associative arrays in Shell scripts
- Is there anything in Zsh like .bash_profile?
- Running programs in parallel using xargs
- How to recursively download a folder via FTP on Linux
- “Error installing rails” because “extconf.rb failed” on Ubuntu 18.04
- Linux find and grep command together
- Java command not found on Linux
- How to output a multiline string in Bash?
- Shell script not running, command not found
- How to ssh from within a bash script?
- Bash script prints “Command Not Found” on empty lines
- Bash mkdir and subfolders
- Bash mkdir and subfolders
- Difference between sh and Bash
- What is a simple explanation for how pipes work in Bash?
- How to include file in a bash shell script
- Error handling in Bash
- Integer expression expected error in shell script
- Delete .DS_STORE files in current folder and all subfolders from command line on Mac
- Error when using ‘sed’ with ‘find’ command on OS X: “invalid command code .”
- How to remove all plugins, posts, pages, and inactive themes in one line with wp-cli? WordPress bloatware removal
- What is the difference between double and single square brackets in bash?
- What are the functional differences between .profile .bash_profile and .bashrc
- How to create a UUID in bash?
- How do I sleep for a millisecond in bash or ksh
- bash: print stderr in red color
- What useful things can one add to one’s .bashrc? [closed]
- How to add a timestamp to bash script log?
- Clean way to write complex multi-line string to a variable
- How can I fully log all bash scripts actions?
- Standard place for user defined bash_completion.d scripts?
- How to remove invalid characters from filenames?
- zcat/gzcat works in linux, not on osx. general linux/osx compatibility