Search This Blog

echo with color in shell script

  • The colour codes:
    Black        0;30     Dark Gray     1;30
    Blue         0;34     Light Blue    1;34
    Green        0;32     Light Green   1;32
    Cyan         0;36     Light Cyan    1;36
    Red          0;31     Light Red     1;31
    Purple       0;35     Light Purple  1;35
    Brown/Orange 0;33     Yellow        1;33
    Light Gray   0;37     White         1;37
  • Example:
    RED='\033[0;31m'
    NO_COLOR='\033[0m'
    echo -e "${RED}Hello World${NO_COLOR}"
    

See also

grep to exclude lines with specific strings

grep with -v (invert) option will exclude the match lines in the output:
cat country.txt | grep -v "United States"

Tabs in bash script

echo with -e option will enable special characters:
echo -e "Name:\tJhon"

Python: check if file or directory exists

  • os.path.exists to check if file or directory exists.
    import os
    os.path.exists('/tmp/1.txt')
    os.path.exists('/tmp')
  • os.path.isfile to check if file exists.
    import os
    os.path.isfile('/tmp/1.txt')

Python: Get file absolute path

Use os.path.abspath function:
import os
os.path.abspath('abc/abc.txt')