rschirin
rschirin

Reputation: 2049

Theory: who can explain the use of =

can someone explain me with this code

 data=$(date +"%Y-%m-%dS%H:%M:%S")
 name="/home/cft/"$data"_test.tar"
 touch $name

works, creating a new .tar file but this code doesn't work

 data=$(date +"%Y-%m-%dS%H:%M:%S")
 name= "/home/cft/"$data"_test.tar"
 touch $name

and gives me this error: no such file or directory?

why the space between = and inverted commas creates this error?

Upvotes: 1

Views: 120

Answers (3)

David W.
David W.

Reputation: 107040

In BASH (and other Bourne type shells like zsh and Kornshell), the equal sign cannot have spaces around it when setting variables.

Good:

$ foo="bar"

Bad:

$ foo= "bar"
$ foo = "bar"

There's no real reason that would prevent spaces from being used. Other programming languages have no problems with this. It's just the syntax of the shell itself.

The reason might be related to the original Bourne shell parsing where the shell would break up a command line based upon whitespace. That would make foo=bar a single parameter instead of two or three (depending if you have white space on both sides or just one side of the equal sign). The shell could see the = sign, and know this parameter is an assignment.

The shell parameter parsing is very primitive in many ways. Whitespace is very important. The shell has to be small and fast in order to be responsive. That means stripping down unessential things like complex line parsing.

Inverted commas I believe you mean quotation marks. Double quotes are used to override the breaking out of parameters over white space:

Bad:

$ foo=this is a test
bash: is: command not found

Good:

$ foo="this is a test"

Double quotes allow interpolation. Single quotes don't:

$ foo="bar"
$ echo "The value of foo is $foo"
The value of foo is bar
$ echo 'The value of foo is $foo'
The value of foo is $foo.

If you start out with single quotes, you can put double quotes inside. If you have single quotes, you can put double quotes inside.

$ foo="bar"
$ echo "The value of foo is '$foo'"
The value of foo is 'bar'
$ echo 'The value of foo is "$foo"'
The value of foo is "$foo"

This means you didn't have to unquote $data. However, you would have to put curly braces around it because underscores are legal characters in variable names. Thus, you want to make sure that the shell understand that the variable is $data and not $data_backup:

name="/home/cft/${data}_test.tar"

Upvotes: 2

chepner
chepner

Reputation: 531325

Shell allows you to provide per-command environment overrides by prefixing the command with one or more variable assignments.

name= "/home/cft/"$data"_test.tar"

asks the shell to run the program named /home/cft/2013-10-08S12:00:00_test.tar (for example) with the value of name set to the empty string in its environment.

(In your case, the error occurs because the named tar file either doesn't exist or, if it does, is not an executable file.)

A variable assignment is identified by having no whitespace after the equal sign.

(name = whatever, of course, is simply a command called name with two string arguments, = and whatever.)

Upvotes: 7

seanmcl
seanmcl

Reputation: 9946

You can't have whitespace between the equal sign and the definition.

http://www.tldp.org/LDP/abs/html/varassignment.html

There is no theory behind this. It's just a decision the language designers made, and which the parser enforces.

Upvotes: 4

Related Questions