How can I concatenate a shell variable to other other parameters in my command lines ?

Use ${ } to enclosure a variable.

Without curly brackets:

VAR="foo"
echo $VAR
echo $VARbar

would give

foo

and nothing, because the variable $VARbar doesn't exist.

With curly brackets:

VAR="foo"
echo ${VAR}
echo ${VAR}bar

would give

foo
foobar

Enclosing the first $VAR is not necessary, but a good practice.

For your example:

#!/bin/sh
WEBSITE="danydiop" 
/usr/bin/mysqldump --opt -u root --ppassword ${WEBSITE} > ${WEBSITE}.sql

This works for bash, zsh, ksh, maybe others too.


Just concatenate the variable contents to whatever else you want to concatenate, e.g.

/usr/bin/mysqldump --opt -u root --ppassword "$WEBSITE" > "$WEBSITE.sql"

The double quotes are unrelated to concatenation: here >$WEBSITE.sql would have worked too. They are needed around variable expansions when the value of the variable might contain some shell special characters (whitespace and \[?*). I strongly recommend putting double quotes around all variable expansions and command substitutions, i.e., always write "$WEBSITE" and "$(mycommand)".

For more details, see $VAR vs ${VAR} and to quote or not to quote.


I usually use quotes, e.g. echo "$WEBSITE.sql".

So you could write it like:

#!/bin/sh
WEBSITE="danydiop" 
/usr/bin/mysqldump --opt -u root --ppassword $WEBSITE > "$WEBSITE.sql"