Concatenate Shell Variable to Other Parameters in Command Lines

shellshell-scriptvariable

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

For example,

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

I need to concatenate .sql to $WEBSITE

Best Answer

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.

Related Question