Linux echo command

echoCommand is one of the most basic and most used commands in Linux. Arguments passed to echowill print to standard output

3 min read
By myfreax
Linux echo command
Linux echo command

echoCommand is one of the most basic and most used commands in Linux. Arguments passed to echowill print to standard output.

echoTypically used in shell scripts to print messages or output the results of other commands.

echois 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/echoprogram, but generally, the shell builtin will take precedence. We'll cover the Bash built-in version echo.

This is echothe syntax of the command echo [-neE] [ARGUMENTS]. When -noptions are used, trailing newlines are suppressed. If -eoptions are specified, the following backslash escape characters will be interpreted:

\\Print the backslash character. \aALERT BEL. \bPrint backspace characters. \cSuppress any further output. \ePrint escape characters.

\fPrint form feeds. \nPrint newlines. \rPrint carriage return. \tPrint horizontal tabs. \vPrint vertical tabs. -Eoption to disable the interpretation of escape characters. This is the default value.

echoThere are a few things to consider when using commands. echoThe shell will replace all variables, wildcards and special characters before passing arguments to the command.

Arguments passed to echoare 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.'.

echoCommands can be used with pattern matching characters such as wildcards. echo The PHP files are: *.phpThe command will print the names of all .phpfiles 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 catcommand to verify and view the contents of the file cat /tmp/file.txt.

echoVariables can also be printed . For example the command echo $USERwill 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 echoto 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 echothe command works. If you have any questions or feedback, feel free to leave a comment