6

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?

2
  • Welcome to AU! What do you mean by "This failed, with an invalid entry"? What makes you this didn't work? – MadMike Jan 24 '17 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 '17 at 14:20
20

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.

3
  • 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 – Sergiy Kolodyazhnyy Jan 24 '17 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 '17 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 – glenn jackman Jan 25 '17 at 1:49
3

@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

3
  • tee is not a builtin thus this solution is slower – Tomáš Nesrovnal Jan 24 '17 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. – Sergiy Kolodyazhnyy Jan 24 '17 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! – Paddy Landau Jan 31 '17 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.