For a file in sysfs, I tried using

echo 45>target_size

to set the value to 45. This failed, with an invalid entry. However, if I do:

x=45;echo $x>target_size

it works fine. Further, if I use a very big number, it gives me an error about having an invalid file number. So apparently when you echo a straight number, the terminal is interpreting that as a file descriptor, rather than a literal number. What is the proper way to echo a number?

  • Welcome to AU! What do you mean by "This failed, with an invalid entry"? What makes you this didn't work?
    – MadMike
    Jan 24, 2017 at 14:14
  • If I write to the sysfs file, it prints a message if it receives something, saying "Attempting to set target size to ___" with whatever you put in, and then if its a valid number it eventually says "Target size now set to ___". But if I just use echo ___, it doesn't print anything. So it's apparently treating $x and the plain number as different, even though they ought to be the same thing.
    – Zephyr
    Jan 24, 2017 at 14:20

2 Answers 2


It's a matter of how the shell parses your command, I think. When you do

echo 45>target_size

the shell redirects file descriptor 45 to filetarget_size and then executes echo with no arguments.

If instead you do

echo 45 >target_size

it redirects the standard output descriptor to file target_size and then executes echo 45.

TL;DR add whitespace before the redirection operator.

  • 2
    Yep, exactly right. Should be worth noting that this seems to be bash-specific. ksh and dash both echo the number into file without issues Jan 24, 2017 at 14:21
  • Just checked in with the bash terminal, and this does indeed seem to be it. That's an interesting quirk that the space matters that much. Thanks!
    – Zephyr
    Jan 24, 2017 at 14:28
  • 2
    Not so quirky: the shell is very whitespace sensitive. Whitespace is used to separate commands/keywords/arguments. You need whitespace within [[ ]] to separate operators/operands Jan 25, 2017 at 1:49

@steeldriver's answer is correct - the space between number and redirection operator matters. It seems to be an inconsistent behavior, as in my tests echo 45>output.txt writes 45 to file on ksh93 and dash shells.

As alternative you could always use tee command with here-string operator <<<.

tee  output_file.txt <<< "45"

Or with those shells that don't have <<< operator, echo 45 | tee output_file.txt

  • tee is not a builtin thus this solution is slower Jan 24, 2017 at 18:04
  • 2
    @TomášNesrovnal slower ? Sorry, but that's a slightly bogus comment. Maybe by nano seconds, but definitelly not any slower than a shell built in. In this case speed is irrelevant, too. Jan 24, 2017 at 18:07
  • 1
    Agreed. Being slower would be important only for a super-critical response time, in which case using a shell would be entirely inappropriate anyway. The days when computers were so slow that they needed maximum optimisation for everyday use are long gone, thank goodness! Jan 31, 2017 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.