Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash "echo" including ">" in the middle creating file - please explain

When I write:

echo 2*3>5 is a valid inequality

In my bash terminal, a new file named 5 is created in my directory which contains:

2*3 is a valid inequality

I want to know what exactly is going on here and why am I getting this output? I believe it's obvious that I'm new to Linux! Thanks

like image 684
secluded Avatar asked Jun 03 '16 20:06

secluded


People also ask

What does echo in bash do?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.

How do I echo a new line in a file?

There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.

How do I echo multiple lines in bash?

To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.

What is echo in shell script?

Echo is a Unix/Linux command tool used for displaying lines of text or string which are passed as arguments on the command line. This is one of the basic command in linux and most commonly used in shell scripts.


2 Answers

In bash, redirections can occur anywhere in the line (but you shouldn't do it! --- see the bash-hackers tutorial). Bash takes the >5 as a redirection, creates output file 5, and then processes the rest of the arguments. Therefore, echo 2*3 is a valid inequality happens, which gives you the output you see in the output file 5.

What you probably want is

echo "2*3>5 is a valid inequality"

or

echo '2*3>5 is a valid inequality'

(with single-quotes), either of which will give you the message you specify as a printout on the command line. The difference is that, within "", variables (such as $foo) will be filled in, but not within ''.

Edit: The bash man page says that the

redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right.

like image 126
cxw Avatar answered Oct 22 '22 01:10

cxw


bash does the output redirection first i.e. >5 is done first and a file named 5 is created (or truncated if it already exists). The resultant file descriptor remains open for the runtime of the echo command.

Then the remaining portion, 2*3 is a valid inequality, runs as the argument to echo and standard output is saved in the (already-open) file 5 eventually.

To get the whole string as the output, use single or double quotes:

echo '2*3>5 is a valid inequality'
like image 27
heemayl Avatar answered Oct 22 '22 00:10

heemayl