Difference between revisions of "Echo Command"
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
(13 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | To display the value of a variable either use echo or printf command as follows: | + | {{navigation |
− | < | + | |previous=Rules for Naming variable name |
+ | |next=Quoting}} | ||
+ | |||
+ | To display the value of a variable, either use echo or printf command as follows: | ||
+ | <syntaxhighlight lang="bash" >echo $varName # not advisable unless you know what the variable contains</syntaxhighlight> | ||
+ | OR (see [[quoting]]): | ||
+ | <syntaxhighlight lang="bash" >echo "$varName"</syntaxhighlight> | ||
OR | OR | ||
− | < | + | <syntaxhighlight lang="bash" >printf "%s\n" "$varName"</syntaxhighlight> |
==Generating Output With echo command== | ==Generating Output With echo command== | ||
− | Use echo command to display a line of text or variable value. It | + | Use echo command to display a line of text or a variable value. It offers no formatting option. It is a good command to display a simple output when you know that the variable's contents will not cause problems. For most uses, printf is preferable. |
===echo Command Examples=== | ===echo Command Examples=== | ||
− | < | + | <syntaxhighlight lang="bash" >#!/bin/bash |
# Display welcome message, computer name and date | # Display welcome message, computer name and date | ||
echo "*** Backup Shell Script ***" | echo "*** Backup Shell Script ***" | ||
Line 27: | Line 33: | ||
echo | echo | ||
echo "*** Backup wrote to $BACKUP/$NOW/latest.tar.gz" | echo "*** Backup wrote to $BACKUP/$NOW/latest.tar.gz" | ||
− | </ | + | </syntaxhighlight> |
+ | ===Printing file names with echo=== | ||
+ | You can also print the file names using [[wildcards]] and echo command: | ||
+ | <syntaxhighlight lang="bash" >cd /etc | ||
+ | echo *.conf</syntaxhighlight> | ||
+ | Sample outputs: | ||
+ | <pre>aatv.conf adduser.conf apg.conf argus.conf atool.conf brltty.conf ca-certificates.conf | ||
+ | chkrootkit.conf cowpoke.conf cvs-cron.conf cvs-pserver.conf dconf.conf dconf-custom.conf | ||
+ | debconf.conf deluser.conf | ||
+ | .... | ||
+ | ... | ||
+ | .. | ||
+ | wodim.conf wpa_supplicant.conf wvdial.conf xorg.conf | ||
+ | </pre> | ||
+ | |||
==Generating Output With printf command== | ==Generating Output With printf command== | ||
printf command format and display data on screen. However, printf does not provide a new line. You need to provide format string using % directives and escapes to format numeric and string arguments in a way that is mostly similar to the C printf() function. Use printf to generate formatted output. | printf command format and display data on screen. However, printf does not provide a new line. You need to provide format string using % directives and escapes to format numeric and string arguments in a way that is mostly similar to the C printf() function. Use printf to generate formatted output. | ||
Line 70: | Line 90: | ||
</pre> | </pre> | ||
+ | Format control string syntax is as follows: | ||
+ | <syntaxhighlight lang="bash" >printf "%w.pL\n" $varName</syntaxhighlight> | ||
+ | Where, | ||
+ | * w - Minimum field width. | ||
+ | * p - Display number of digits after the decimal point (precision). | ||
+ | * L - a conversion character. It can be: | ||
+ | ** s - String | ||
+ | ** d - Integer | ||
+ | ** e - Exponential | ||
+ | ** f - Floating point | ||
===printf Command Examples=== | ===printf Command Examples=== | ||
Type the following at a shell prompt: | Type the following at a shell prompt: | ||
− | < | + | <syntaxhighlight lang="bash" >vech="Car" |
printf "%s\n" $vech | printf "%s\n" $vech | ||
printf "%1s\n" $vech | printf "%1s\n" $vech | ||
Line 83: | Line 113: | ||
no=10 | no=10 | ||
printf "%d\n" $no | printf "%d\n" $no | ||
− | big= | + | big=5355765 |
− | printf "%e\n" $ | + | printf "%e\n" $big |
− | printf "%5.2e\n" $ | + | printf "%5.2e\n" $big |
+ | sales=54245.22 | ||
+ | printf "%f\n" $sales | ||
+ | printf "%.2f\n" $sales</syntaxhighlight> | ||
+ | [[Category:Variables and Quoting]] | ||
+ | {{navigation | ||
+ | |previous=Rules for Naming variable name | ||
+ | |next=Quoting}} |
Latest revision as of 22:50, 29 March 2016
← Rules for Naming variable name • Home • Quoting →
To display the value of a variable, either use echo or printf command as follows:
echo $varName # not advisable unless you know what the variable contains
OR (see quoting):
echo "$varName"
OR
printf "%s\n" "$varName"
Generating Output With echo command
Use echo command to display a line of text or a variable value. It offers no formatting option. It is a good command to display a simple output when you know that the variable's contents will not cause problems. For most uses, printf is preferable.
echo Command Examples
#!/bin/bash
# Display welcome message, computer name and date
echo "*** Backup Shell Script ***"
echo
echo "*** Run time: $(date) @ $(hostname)"
echo
# Define variables
BACKUP="/nas05"
NOW=$(date +"%d-%m-%Y")
# Let us start backup
echo "*** Dumping MySQL Database to $BACKUP/$NOW..."
# Just sleep for 3 secs
sleep 3
# And we are done...
echo
echo "*** Backup wrote to $BACKUP/$NOW/latest.tar.gz"
Printing file names with echo
You can also print the file names using wildcards and echo command:
cd /etc
echo *.conf
Sample outputs:
aatv.conf adduser.conf apg.conf argus.conf atool.conf brltty.conf ca-certificates.conf chkrootkit.conf cowpoke.conf cvs-cron.conf cvs-pserver.conf dconf.conf dconf-custom.conf debconf.conf deluser.conf .... ... .. wodim.conf wpa_supplicant.conf wvdial.conf xorg.conf
Generating Output With printf command
printf command format and display data on screen. However, printf does not provide a new line. You need to provide format string using % directives and escapes to format numeric and string arguments in a way that is mostly similar to the C printf() function. Use printf to generate formatted output.
printf Format Directives
From the printf man page:
FORMAT controls the output as in C printf. Interpreted sequences are: \" double quote \NNN character with octal value NNN (1 to 3 digits) \\ backslash \a alert (BEL) \b backspace \c produce no further output \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \xHH byte with hexadecimal value HH (1 to 2 digits) \uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits) \UHHHHHHHH Unicode character with hex value HHHHHHHH (8 digits) %% a single % %b ARGUMENT as a string with '\' escapes interpreted, except that octal escapes are of the form \0 or \0NNN and all C format specifications ending with one of diouxXfeEgGcs, with ARGUMENTs converted to proper type first. Variable widths are handled.
Format control string syntax is as follows:
printf "%w.pL\n" $varName
Where,
- w - Minimum field width.
- p - Display number of digits after the decimal point (precision).
- L - a conversion character. It can be:
- s - String
- d - Integer
- e - Exponential
- f - Floating point
printf Command Examples
Type the following at a shell prompt:
vech="Car"
printf "%s\n" $vech
printf "%1s\n" $vech
printf "%1.1s\n" $vech
printf "%1.2s\n" $vech
printf "%1.3s\n" $vech
printf "%10.3s\n" $vech
printf "%10.1s\n" $vech
no=10
printf "%d\n" $no
big=5355765
printf "%e\n" $big
printf "%5.2e\n" $big
sales=54245.22
printf "%f\n" $sales
printf "%.2f\n" $sales