bashfunctionquotingoutput-redirect

Create Bash function which echo's into a file (syntax?)


Whats the proper syntax for the below function in bashrc, which will echo a number into a file?

I've tried:

brightness(){
    "echo '$1' > /sys/class/backlight/intel_backlight/brightness"
}

I've tried many variations, but in this example when calling the function, I get:

brightness:1: no such file or directory: echo '250' > /sys/class/backlight/intel_backlight/brightness

Solution

  • The quotes were placed incorrectly. It should be:

    brightness(){
        echo "$1" > /sys/class/backlight/intel_backlight/brightness
    }
    

    Btw, since the file is only writable by root, you need to use sudo (unless you are root). tee comes in handy here:

    brightness(){
        echo "$1" | sudo tee /sys/class/backlight/intel_backlight/brightness > /dev/null
    }