LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Assign command output to a varible. (http://www.linuxquestions.org/questions/linux-newbie-8/assign-command-output-to-a-varible-306302/)

wilku 03-26-2005 10:06 AM

Assign command output to a varible.
 
Hello

I'm trying to write a bash script that would dump mysql database (script run by cron once a week). I need to name the dump file with a current date like twilk_20050326.sql. I come to a problem at the very beginning trying to create filename varible:
Code:

FILE_NAME=date +twilk_%G%m%d.sql
This one gives an error of command not found: +twilk_%G%m%d.sql
I also tried without a space before "+", with single quotes, double quotes around "date ... .sql". All of them assign value of a string, not executing command.

Thanks for any help

wapcaplet 03-26-2005 10:08 AM

Try this:

Code:

FILE_NAME=$( date +twilk_%G%m%d.sql )
or this:

Code:

FILE_NAME=`date +twilk_%G%m%d.sql`
Both of those execute the command, and assign the command's output to FILE_NAME.

wilku 03-26-2005 10:13 AM

The first solution works. Thanks!

The second one, with single quotes, doesn't (at least for me). It assignes string value instead of executing command.

wapcaplet 03-26-2005 11:56 AM

Those are backquotes in the second example (the key next to the "1" on most U.S. keyboards). They serve the same purpose as the first example; the first one is bash-specific, though, so the backquotes one may be more portable to other scripting environments (if you're concerned at all about that). I prefer the first way, since it's easier to read.


All times are GMT -5. The time now is 02:05 PM.