Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why can't I use echo $1 > /sys/class/backlight/acpi_video0/brightness in a simple bash script?

It gives me the error: echo: write error: Invalid argument.

share|improve this question
    
Having the same issue while trying to do the same thing. I've tried things like function brightness { bright=$1; sudo su -c 'echo "$bright" > /sys/class/backlight/acpi_video0/brightness'; } too, but I still haven't figured it out. – hangtwenty Nov 2 '12 at 12:15
up vote 1 down vote accepted

Try echo "$1" > /sys/class/backlight/acpi_video0/brightness.

I bet the shell is expanding $1 and thus echo thinks it is receiving a bunch of arguments, rather than a string.

share|improve this answer

You should check what the actual value of $1 is. This error means you are trying to write an invalid value -- either it's out of range or just in general not a meaningful value.

At a glance, it appears that it accepts an integer in the range 0 to 8 (for me at least).

share|improve this answer

Try using let

#!/bin/bash

POLKU='/sys/class/backlight/radeon_bl0/brightness'


if [ $# -eq "0" ]
    then
        echo 100 > $POLKU
    else
        let gg=$1
        echo $gg > $POLKU
fi
share|improve this answer

That file is a special file. It cannot be written to if what is written is not solely a number. If you try writing a number with echo, you will get a newline character at the end. echo -n solves the problem.

EDIT: Also, you might having the problem which I just had; that you need to be root and sudo won't help you for whatever reason, making it very tedious to type su; <your command>; exit all the time. For this I made an (overly ambitious) python script:

#!/usr/bin/python

from sys import *

PATH = "/sys/class/backlight/intel_backlight/brightness"

if len(argv) != 2:
    print("Usage: bright.py <brightness>")
    exit()

try:
    brightness = int(argv[1])
    if not 0 <= brightness <= 825:
        raise Exception()
except:
    print("<brightness> must be an integer between 0 and 825.")
    exit()

if brightness == 0:
    readString = raw_input("A value of 0 will turn off your screen. Are you sure you want to continue? [y/N] ")
    if readString != "y":
        exit()
elif brightness <= 5:
    with open(PATH, "r") as f:
        oldBrightness = int(f.read())
        if brightness < oldBrightness:
            readString = raw_input("A value of %i will make your screen very dark. Are you sure you want to continue? [y/N] " % brightness)
            if readString != "y":
                exit()

try:
    with open(PATH, "w") as f:
        f.write(str(brightness))
except:
    print("Failed to write to file. Are you root?")
    exit()
share|improve this answer

You must log in to answer this question.

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