tags:

views:

53

answers:

6

I am trying to do a code walk through a badly written bash script.

I have come across this statement:

FOOBAR_NAME=`date +WeekNo.%W`

There are no prior declarations of any of the RHS variables in the script, lines preceding this statement.

So my question is:

What does FOOBAR_NAME resolve to, when it is used a few lines down in the script as $FOOBAR_NAME ?

+3  A: 

Try it!

$date +WeekNo.%W
WeekNo.30
Karussell
curse you and your Ninja powers! +1 for being 33 seconds faster than me.
Wayne Werner
lol :-) .......
Karussell
+2  A: 

There are no variables being referenced in the RHS.

The backtick operator (`) evaluates its contents and returns the output, similar (identical?) to $(). It's a quick way to write an eval (in other languages).

Type date +WeekNo.%W in a shell. What is printed (in stdout, with newlines collapsed) is what will be stored in FOOBAR_NAME.

Note that the evaluation occurs only once, which is during the assignment. date isn't executed each time you reference FOOBAR_NAME.

strager
+1 for the explanation ..
morpheous
A: 

See man date for a description of the date command and it's formatting options. %W is week number.

Peter van der Heijden
A: 

This is using a format string to the date command to create the a string that contains the week number.

The backticks execute the command between them; and the line assigns the result to the shell variable FOOBAR_NAME.

So if you really want to know what it does, just cut and paste the text between the `` into a shell and execute it.

Recurse
It's called `backquotes`
Anders
@Anders, I call them backticks, and apparently others like @Recurse and @eje211 do as well.
strager
@strager, Does not make it correct does it? Please read http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
Anders
@Anders: Many characters go by multiple names: `!`: "bang", "exclamation mark/point", etc., `*`: "splat", "star", "asterisk", etc., `#`: "pound", "hash", etc.
Dennis Williamson
@Dennis Williamson, Yes I know that, does not make it more correct either. Trying to follow a standard will make life a lot easier for everybody.
Anders
@Anders: Who's going to decide which of "period", "full stop", "point", "dot", etc. is more correct? And is it a "backquote" or a "grave accent"?
Dennis Williamson
The pedantically correct term for the character is <code>grave accent</code>. Everything else is just a convenience, and which one is preferred varies from country to country and person to person.
Recurse
A: 

You can find the answer in man date: If you specify an argument starting with +, then the rest of that argument is taken as a format string. The Weekno. part is taken literally, the %W does:

%W week number of year, with Monday as first day of week (00..53)

Carl Smotricz
A: 

The assignment operator ("=") assigns the value on its right part to a variable on the left part. Here the variable is FOOBAR_NAME.

The right part is a subshell. The backticks ("`") create a subshell. The output of that subshell will go to the variable.

The subshell rurns the Unix date command. The manual page for all Unix commands is on the Internet. There a Unix man page for date. Here, %W will be replaced by the number of the week.

So the variable gets the value "WeekNo" plus the number of the week.

eje211