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

Just how can I concatenate a shell variable to other various other parameters in my command lines?

As an example,

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

I require to concatenate .sql to $WEBSITE

0
2019-05-13 05:01:04
Source Share
Answers: 3

Just concatenate the variable materials to whatever else you intend to concatenate, as an example

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

The double quotes are unconnected to concatenation : below >$WEBSITE.sql would certainly have functioned also. They are required around variable developments when the value of the variable could have some shell unique personalities (whitespace and also \[?*). I highly advise placing double quotes around all variable developments and also command replacements, i.e., constantly write "$WEBSITE" and also "$(mycommand)".

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

0
2019-05-18 01:10:18
Source

I generally make use of quotes, as an example echo "$WEBSITE.sql".

So you can write it like:

#!/bin/sh
WEBSITE="danydiop" 
/usr/bin/mysqldump --opt -u root --ppassword $WEBSITE > "$WEBSITE.sql"
0
2019-05-17 18:15:24
Source

Use ${ } to enclosure a variable.

Without curly brackets:

VAR="foo"
echo $VAR
echo $VARbar

would certainly offer

foo

and also absolutely nothing, due to the fact that the variable $VARbar does not exist.

With curly brackets:

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

would certainly offer

foo
foobar

Enclosing the first $VAR is not essential, yet an excellent technique.

For your instance:

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

This benefits bash, zsh, ksh, possibly others also.

0
2019-05-17 18:13:33
Source