What does space mean in bash?

I have something like this:

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

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

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

It is not right. The difference is the space after the variable.

I know that without a variable there are no spaces directly. But what is the meaning of a space after an equal sign. Does any space in the space have special meaning?

+5
source share
2 answers

Variable assignment

Syntax assignment of variables :

 name=value 

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

 name='value with spaces or special characters' 

or with double quotes for variable expansion :

 name="stringA $variable stringB" 

If quotation marks are missing, the second word in the meaning part is interpreted as a command. In fact, this is a way to pass environment variables to a command (see below).

If there is no value, a variable with an empty value is created.

Environment Variables

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

 nameA=valueA nameB=valueB nameC=valueC command arguments 

name-value pairs are separated by spaces.

Example

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

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

Your teams

So your command:

 projectName= echo $tempPBXProjFilePath 

actually means that you invoke the echo command with the expand arguments from $tempPBXProjFilePath and set the projectName environment projectName to an empty value.

And this command:

 projectName=echo $tempPBXProjFilePath 

sets the projectName environment projectName to the echo string and invokes the command extended from the $tempPBXProjFilePath variable.

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

+3
source

Bash divides each line into words in each space (spaces or tabs).

The first word that he finds is the name of the executable command, and the remaining words become the arguments of this command.

so when you pass

 projectName=echo 

Bash understand projectName = echo as a variable assignment and

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

as a team! (as Chris Dodd pointed out)

Spaces

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

INCORRECT 1

 example = Hello 

INCORRECT 2

 example= Hello 

WRONG 3

 example =Hello 

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

CORRECT 1

 example=Hello 

CORRECT 2

 example=" Hello" 

You can see more:

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

+2
source

Source: https://habr.com/ru/post/1261969/


All Articles