Reputation: 517
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
Upvotes: 8
Views: 536
Reputation: 42017
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'
Upvotes: 7
Reputation: 17041
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.
Upvotes: 12
Reputation: 95654
This is an example of output redirection. You're instructing the echo statement to, instead of writing to standard out, write to a filename. That filename happens to be "5".
You can avoid that behavior by quoting:
echo "2*3>5 is a valid inequality"
Upvotes: 3