Technical skill

Linux command: echo

Subject: .../LINUX

Print a message from a command

A program may need to show a short message or print a value for another command to read. The shell command echo writes its arguments to standard output, the normal output stream that often appears in the terminal. It usually separates several arguments with spaces and adds a newline. For example, echo 'Service check started' prints a fixed message, while echo "$HOME" prints the value of the HOME variable. The shell expands HOME before running the command, and the quotes keep a value with spaces together.

Know which echo you are running

The name echo may refer to a command built into the shell or to a separate utility. Bash has a builtin, and GNU Coreutils also supplies an external program; the shell normally uses its builtin first. In Bash, type -a echo lists the available definitions. Options may work differently across implementations, so scripts should not depend on an extension unless they deliberately call that implementation.

The familiar options are -n, which leaves off the final newline, and -e, which asks some implementations to interpret backslash escapes such as \n. POSIX does not require options for echo. If the first argument is -n, or an argument contains a backslash, the standard lets each implementation choose the result. Bash's xpg_echo setting also affects how its builtin handles escapes. The same text can therefore print -n literally in one environment and treat it as an option in another, or preserve a backslash in one shell and convert it in another.

Use printf when the output must be exact

For controlled formatting, the shell builtin printf takes a fixed format string followed by data arguments:

message='Build completed'
printf '%s\n' "$message"
printf 'Port: %s\n' "$port"

%s prints a string argument, and \n adds a newline. Keep the format string fixed. If user-provided text becomes the format string, its percent signs may be treated as formatting instructions. Pass variable data as a separate argument instead.

Shell quoting and printed formatting happen at different steps. Quotes keep a value from splitting into several arguments or expanding as a filename pattern before echo or printf runs. printf then controls how the value is written. Neither command removes terminal control characters from arbitrary text, so take care when displaying data from an untrusted source. echo is concise for a fixed message; use printf when exact bytes or portable behavior matter, and check the shell's documentation for implementation-specific options.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful