1

On the terminal, typing echo -n will not output anything at all, if echo has no parameters, then why using parameter -n does nothing in echo at all? Note: Doing something like echo nil -n nil will work and output nil -n nil, so if echo outputs everything, then why doesn't it output -n, it should output -n not doing nothing.

2 Answers 2

5

By default echo appends a newline character to it's output, so if you issue echo without any parameters, the output is nothing, followed by a newline.

The -n option means 'do not output the trailing newline'. So when you issue echo -n it uses the -n as an option, and outputs nothing without a newline. This will leave no evidence of output on the display.

If you use a letter which is not a valid option for echo such as '-x' it will be printed to the screen, with a newline as normal. If you use -n within other input, it is not read as an option, so will print normally.

2

To add to Arronical's answer, echo does not support --, which is the standard convention to declare "This is end of options/flags, consider everything that follows as arguments."

GNU echo can be made to output -n by using

echo -e "\e--n"

In zsh, echo is a shell built in, and echo - -n does the job.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .