gingerdev
gingerdev

Reputation: 139

BASH automatically adding quotes to string

I'm trying to write a simple bash script that executes a command with one string variable. Upon execution bash adds single quotes to the string variable making the command useless. How do I execute the command without the quotes from the bash script?

#!/bin/bash
key=$(echo $1 | tr '[:lower:]' '[:upper:]')
sudo tee /proc/acpi/bbswitch \<\<\<$key

the output I get is

~/scripts$ bash -x nvidia on
++ echo on
++ tr '[:lower:]' '[:upper:]'
+ key=ON
+ sudo tee /proc/acpi/bbswitch '<<<ON'

the two commands I want to run without the quotes are either

sudo tee /proc/acpi/bbswitch <<<ON

or

sudo tee /proc/acpi/bbswitch <<<OFF

Upvotes: 0

Views: 1083

Answers (2)

chepner
chepner

Reputation: 532238

There's no need to quote the <<< operator. sudo doesn't read from its standard input by default; it passes it through to the command it runs.

sudo tee /proc/acpi/bbswitch <<< $key

Upvotes: 0

Barmar
Barmar

Reputation: 782409

The problem isn't the quotes, it's that sudo doesn't execute the command via the shell. So metacharacters like <<< don't have any special meaning when they're given as sudo arguments. You need to invoke the shell explicitly:

sudo bash -c "tee /proc/acpi/bbswitch <<<$key"

But there doesn't really seem to be a need to use a here-string for this. Just use:

echo "$key" | sudo tee /proc/acpi/bbswitch

Upvotes: 3

Related Questions