简体   繁体   English

bash在bash中实际上意味着什么?

[英]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 . 我想从$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? 在空白处有特殊含义的地方吗?

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 . 在调用/bin/ls之前,该命令将LD_PRELOAD环境变量分配给/path/to/my/malloc.so

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. 实际上意味着您使用从$tempPBXProjFilePath 扩展的参数调用echo命令,并将projectName环境变量设置为空值。

And this command: 而这个命令:

projectName=echo $tempPBXProjFilePath

sets projectName environment variable to echo string, and calls a command expanded from $tempPBXProjFilePath variable. projectName环境变量设置为echo字符串,并调用从$tempPBXProjFilePath变量扩展的命令。

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" . 为了防止重新解释特殊字符,应使用弱引用: "$variable" And if you want to prevent even variable expansion in a string value, use single quotes: 'some value' . 而且,如果要防止字符串值中的变量扩展,请使用单引号: 'some value'

Bash divides each line into words at each whitespace (spaces or tabs). Bash将每一行划分为每个空白(空格或制表符)处的单词。

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 bash将projectName = echo理解为变量分配,并且

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

as a command! 作为命令! (as pointed by Chris Dodd) (如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 错误1

example = Hello

INCORRECT 2 错误2

example= Hello

INCORRECT 3 错误3

example =Hello

The only valid form is no spaces between the variable name and assigned value: 唯一有效的形式是变量名称和赋值之间没有空格:

CORRECT 1 正确1

example=Hello

CORRECT 2 正确2

example=" Hello"

You can see more at: 您可以在以下位置看到更多信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM