Quantcast
Channel: Commands by xakon
Viewing all articles
Browse latest Browse all 6

Multiple variable assignments from command output in BASH

$
0
0
$ eval $(date +"day=%d; month=%m; year=%y")

It's quite easy to capture the output of a command and assign it in a shell's variable:

day=$(date +%d) month=$(date +%m)

But, what if we want to perform the same task with just one program invocation? Here comes the power of eval! date(1) outputs a string like "day=29; month=07; year=11" (notice the semicolons I added on purpose at date's custom output) which is a legal shell line. This like is then parsed and executed by the shell once again with the help of eval. Just setting 3 variables!

Inspired by LinuxJournal's column "Dave Taylor's Work the Shell".

commandlinefu.com

Diff your entire server config at ScriptRock.com


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images