Linux echo command
echoCommand is one of the most basic and most used commands in Linux. Arguments passed to echowill print to standard output
echo
Command is one of the most basic and most used commands in Linux. Arguments passed to echo
will print to standard output.
echo
Typically used in shell scripts to print messages or output the results of other commands.
echo
is a built-in shell command. echo behaves like other popular shells like Zsh and Ksh. But their behavior differs slightly from shell to shell.
There is also a separate /usr/bin/echo
program, but generally, the shell builtin will take precedence. We'll cover the Bash built-in version echo
.
This is echo
the syntax of the command echo [-neE] [ARGUMENTS]
. When -n
options are used, trailing newlines are suppressed. If -e
options are specified, the following backslash escape characters will be interpreted:
\\
Print the backslash character. \a
ALERT BEL. \b
Print backspace characters. \c
Suppress any further output. \e
Print escape characters.
\f
Print form feeds. \n
Print newlines. \r
Print carriage return. \t
Print horizontal tabs. \v
Print vertical tabs. -E
option to disable the interpretation of escape characters. This is the default value.
echo
There are a few things to consider when using commands. echo
The shell will replace all variables, wildcards and special characters before passing arguments to the command.
Arguments passed to echo
are enclosed in double or single quotes. Although not necessary, it is a good programming practice.
When single quotes are used ''
, the literal value of each character enclosed in the quotes is preserved. Variables and commands are not interpreted.
echo example
echo Hello, World!
This will print a line of Hello, World! text on standard output. This will be your easiest introduction to how to use the echo.
To print double quotes, enclose them in single quotes, such as a command echo 'Hello "myfreax"'
.
Another way is to escape it with a backslash character. For example command echo "Hello \"myfreax\""
.
If you need to print a single quote, enclose it in double quotes, such as a command echo "I'm a Linux user."
.
Another way is to use ANSI-C quotes . For example command echo $'I'm a Linux user.'
.
echo
Commands can be used with pattern matching characters such as wildcards. echo The PHP files are: *.php
The command will print the names of all .php
files in the current directory with an extension.
Redirecting to a file is the opposite of printing output to the screen, you can redirect the output of echo to a file using the >
, >>
redirection operator. For example command echo -e 'g.\nSocrates' >> /tmp/file.txt
.
If file.txt does not exist, the command will create it. >
Will be overwritten when the file is used, and >>
the output will be appended to the file. You can use the make cat
command to verify and view the contents of the file cat /tmp/file.txt
.
echo
Variables can also be printed . For example the command echo $USER
will print the name of the currently logged in user. is a shell variable$USER
that holds the username .
To print the output of a command , you can use $(command)
an expression echo
to include the command output in the command parameter. For example the command echo "The date is: $(date +%D)"
will print the current date.
echo Hello, World! #Simplest usage
echo 'Hello "myfreax"' #equivalence: echo "Hello \"myfreax\""
echo "I'm a Linux user." #equivalence: echo $'I'm a Linux user.'
echo The PHP files are: *.php #Use wildcards
echo -e 'g.\nSocrates' >> /tmp/file.txt #redirect to file
echo $USER #print environment variable
echo "The date is: $(date +%D)" #print the output of another command
echo print in color
You can use ANSI escape sequences to change the foreground and background colors or to set text attributes such as underline and bold.
echo -e "\033[1;37mWHITE"
Print white, echo -e "\033[0;30mBLACK"
print black. echo -e "\033[0;34mBLUE"
Print blue. echo -e "\033[0;32mGREEN"
Print green.
echo -e "\033[0;36mCYAN"
Print cyan. echo -e "\033[0;31mRED"
Print red. echo -e "\033[0;35mPURPLE"
Print purple.
echo -e "\033[0;33mYELLOW"
Print yellow. echo -e "\033[1;30mGRAY"
Print gray.
conclusion
By now you should have a good understanding of how echo
the command works. If you have any questions or feedback, feel free to leave a comment