To print a string or a text on the terminal, you can use the echo
command followed by the string or the text in the double quotes ""
symbol.
TL;DR
# Print string of `Hello World` in terminal
echo "Hello World"
For example, let's say we have to print the string Hello World
in the terminal. To do that, we can use the echo
command followed by the text Hello World
inside the ""
symbol.
It can be done like this,
# Print string of `Hello World` in terminal
echo "Hello World"
After executing the above command, you can see the Hello World
is output on the next line in the terminal.
It will look like this,
# Output
Hello World
To print without a newline in the terminal
To print the same string but without a new line there are 2 ways to do it:
- The first one is to use the
-n
flag after the echo command. The-n
flag is to not print the trailing newline character.
The command will look like this,
# Print string of `Hello World` in terminal
# without a new line using the `-n` flag
echo -n "Hello World"
- The second way is to add the
\c
characters at the end of the string you need to print. This will instruct theecho
command to not jump to a new line.
The command will look like this,
# Print string of `Hello World` in terminal
# without a new line by appending the
# `\c` characters at the of the string to print
echo "Hello World\c"
That's all 😃.