print in terminal

    A terminal is an interactive tool through which the user can interact with the shell environment.

    echo is used to print in terminal. By default a new line starts after each echo call. The information following echo can be unquoted, double-quoted, or single-quoted. You cannot have an exclamation mark in double quotes, but you can have an exclamation mark in single quotes. Either remove the quotes, or use the escape symbol \.

    When using echo without quotes, commas cannot be used because commas are delimiters between commands. echo hello; hello is 2 commands. Variable substitution does not work within single quotes.

    printf can also be used to print in the terminal. printf accepts quoted text or arguments, separated by spaces. The command can also use format strings. By default it does not generate new lines.

#!/bin/bash 

#Filename: printf.sh

printf  "%-5s %-10s %-4s\n" No Name  Mark 

printf  "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456 

printf  "%-5s %-10s %-4.2f\n" 2 James 90.9989 

 

printf  "%-5s %-10s %-4.2f\n" 3 Jeff 77.564

------------------------------Results of the------------------ ------------------------------

No    Name       Mark

1     Sarath     80.35

2     James      91.00

3     Jeff       77.56

 

    %s, %c, %d, and %f are format substitution characters. The - in %-5s means left-aligned, 5 is the width, if no - is specified, the string is right-aligned, and if the specified width is insufficient, it is filled with spaces.

    Remember to put flags before the string in the command.

    There will be a new line at the end of the echo output. If you don't want this, you can use the -n flag. When using escape sequences, use echo -e, for example: echo -e "1\t2\t3"

    Printing output in color is accomplished using escape sequences. Colors are represented by color codes. For example reset=0,black=30,red=31,green=32,yellow=33,blue=34,magenta=35,cyan=36,white=37.

The \e[1;31m in echo -e "\e[1;31m This is red text \e[0m" is an escape string to set the color to red,

\e[0m restores the color to its original state.

    Common background color codes are: reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, 

magenta = 45, cyan = 46, and white=47。例如:

echo -e "\e[1;42m Green Background \e[0m"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037502&siteId=291194637