Tepmnthar
Tepmnthar

Reputation: 557

What does whitespace actually mean in bash?

I have something like this:

projectName= echo $tempPBXProjFilePath | sed "s/.*\/\(.*\)\.xcodeproj.*$/\1/g";

I want to extract substring from $tempPBXProjFilePath. And this is correct. However, if I write it like this:

projectName=echo $tempPBXProjFilePath | sed "s/.*\/\(.*\)\.xcodeproj.*$/\1/g";

It is wrong. The difference is the whitespace after the variable.

I know there is no whitespace after variable directly. But what's the meaning of the whitespace after equal-sign. Is there any place whitespace has special meaning?

Upvotes: 5

Views: 1551

Answers (2)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21502

Variable Assignment

The syntax for variable assignment is:

name=value

Note, there are no spaces around the = sign. If the value has spaces, or special characters, it should be quoted with single quotes:

name='value with spaces or special characters'

or with double quotes for variable expansion:

name="stringA $variable stringB"

If quotes are missing, the second word in the value part is interpreted as a command. Actually, this is a way to pass environment variables to a command (see below).

If the value is missing, a variable with an empty value is created.

Environment Variables

There is another syntax that allows to assign environment variables for a command:

nameA=valueA nameB=valueB nameC=valueC command arguments

The name-value pairs are separated with space characters.

Example

LD_PRELOAD=/path/to/my/malloc.so /bin/ls

The command assigns LD_PRELOAD environment variable to /path/to/my/malloc.so before invoking /bin/ls.

Your Commands

Thus, your command:

projectName= echo $tempPBXProjFilePath

actually means that you call echo command with arguments expanded from $tempPBXProjFilePath, and set projectName environment variable to an empty value.

And this command:

projectName=echo $tempPBXProjFilePath

sets projectName environment variable to echo string, and calls a command expanded from $tempPBXProjFilePath variable.

Note, if a variable is not enclosed in double quotes, the special characters that present in its value are interpreted by the shell. In order to prevent reinterpretation of the special characters, you should use weak quoting: "$variable". And if you want to prevent even variable expansion in a string value, use single quotes: 'some value'.

Upvotes: 3

Leonardo Hermoso
Leonardo Hermoso

Reputation: 838

Bash divides each line into words at each whitespace (spaces or tabs).

The first word it finds is the name of the command to be executed and the remaining words become arguments to that command.

so when you pass

projectName=echo

bash understand projectName=echo as a variable assignment, and

$tempPBXProjFilePath | sed "s/.*\/\(.*\)\.xcodeproj.*$/\1/g";

as a command! (as pointed by Chris Dodd)

Whitespace

Putting spaces on either or both sides of the equal-sign (=) when assigning a value to a variable will fail.

INCORRECT 1

example = Hello

INCORRECT 2

example= Hello

INCORRECT 3

example =Hello

The only valid form is no spaces between the variable name and assigned value:

CORRECT 1

example=Hello

CORRECT 2

example=" Hello"

You can see more at:

http://wiki.bash-hackers.org/scripting/newbie_traps

Upvotes: 2

Related Questions