Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does whitespace actually mean in bash?

Tags:

linux

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?

like image 619
Tepmnthar Avatar asked Dec 28 '16 02:12

Tepmnthar


People also ask

What is whitespace in bash?

Whitespace — this is a tab, newline, vertical tab, form feed, carriage return, or space. Bash uses whitespace to determine where words begin and end. The first word is the command name and additional words become arguments to that command.

Does whitespace matter in bash?

The lack of spaces is actually how the shell distinguishes an assignment from a regular command. Also, spaces are required around the operators in a [ command: [ "$timer"=0 ] is a valid test command, but it doesn't do what you expect because it doesn't recognize = as an operator.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

Is bash whitespace sensitive?

Bash indenting is very sensitive to characters. For example a space behind “do” in while/for loops will throw it of. When you have nested loops this is very ugly, and makes it hard to follow the code.

What does [ [ [space] mean in Bash?

One of those is [:space:], which matches whitespace characters (like s in Perl regexes). See e.g. Pattern Matching in Bash's manual So, [ [:space:]] is a part of a regular expression or pattern match, one that matches just whitespace. E.g. a pattern match (standard shell, not Bash-specific):

How do you match whitespace in regular expressions?

In regular expressions and filename globs/shell patterns, the [...] construct matches any one character of those listed within the brackets. Within those brackets, a number of named standard character character classes can be used. One of those is [:space:], which matches whitespace characters (like \s in Perl regexes).

How many spaces are there in a bash string?

If you (happen to?) search for the string " space" (that is, a space followed by the word "space") in the online bash manual, there are "only" about 32 matches to go through. About the tenth one will be here:

What characters can be classified as white space characters?

Define characters to be classified as white-space characters. In the POSIX locale, exactly <space>, <form-feed>, <newline>, <carriage-return>, <tab>, and <vertical-tab> shall be included. In a locale definition file, no character specified for the keywords upper, lower, alpha, digit, graph, or xdigit shall be specified.


1 Answers

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'.

like image 183
Ruslan Osmanov Avatar answered Sep 18 '22 17:09

Ruslan Osmanov