demboz11
demboz11

Reputation: 937

echo variable result in quotes BASH

I have a bash script like this:

#!/bin/bash
pavadinimas=$1
pavadinimas2=${pavadinimas::-4}
echo "#!/bin/sh
mysql -uUSER -pPASSWORD -DDatabase -e 'UPDATE boom SET count = count + 1 WHERE Failo_vardas="$pavadinimas"';
vlc -f --play-and-exit /var/www/html/uploads/$pavadinimas" > /var/www/html/script/"$pavadinimas2.sh"

And I'm having problem with this line:

mysql -uUSER -pPASSWORD -DDatabase -e 'UPDATE boom SET count = count + 1 WHERE Failo_vardas="$pavadinimas"';

As you see I want to add the variable to quotes, but It comes out without It. I tried a lot of combinations to solve this out, but I failed. Lack of experience :/

Script result:

#!/bin/sh

    mysql -uUSER -pPASSWORD -DDatabase -e 'UPDATE boom SET count = parodymai + 1 WHERE Failo_vardas=name.mp4';
    vlc -f --play-and-exit /var/www/html/uploads/gaidys.mp4

I want to echo the variable in quotes like this:

mysql -uUSER -pPASSWORD -DDatabase -e 'UPDATE boom SET count = count + 1 WHERE Failo_vardas="name.mp4"';

Upvotes: 0

Views: 4560

Answers (2)

Gavin
Gavin

Reputation: 4515

You are really close. You just have to escape the quotes that you want to use. e.g. WHERE Failo_vardas=\"$pavadinimas\"

Upvotes: 2

Jean-François Fabre
Jean-François Fabre

Reputation: 140148

You have to leave the single quoting or your variable won't be evaluated.

So insert a single quote after the double quote, put your variable to evaluate, and re-insert a quote after your variable. Where the single-quoting ends, your env. variable will be evaluated instead of being treated literally.

Demo:

$ pavadinimas=name.mp4

$ echo 'UPDATE boom SET count = count + 1 WHERE Failo_vardas="'$pavadinimas'"';

result:

UPDATE boom SET count = count + 1 WHERE Failo_vardas="name.mp4"

Upvotes: 1

Related Questions