Scott Frazer
Scott Frazer

Reputation: 2185

BASH string behavior oddity

Why is branch being set to 'm' rather than 'master' in this example?

$ branch="[master]"
$ echo $branch
m

This happens from any directory I am in, but only on my mac and not on one of my linux boxes.

Upvotes: 0

Views: 80

Answers (3)

Victor Zamanian
Victor Zamanian

Reputation: 3190

Indeed, just like unwind says, this has to do with file name expansion.

[15:33] ~$ branch="[master]"
[15:33] ~$ echo $branch 
[master]
[15:33] ~$ touch m
[15:33] ~$ echo $branch
m
[15:33] ~$

There must be a file or directory in your current directory named m.

A possible remedy for this is to use quoting:

$ ls m # `m' exists
m
$ echo "$branch" # yet this echoes "[master]"
[master]

Upvotes: 1

bratkartoffel
bratkartoffel

Reputation: 1177

Maybe some kind of escaping issue, have you tried to escape the [ and ]?

Edit: I can reproduce the behaviour on my system, but only if a file named m exists:

[sf@zeus:~] touch m
[sf@zeus:~] branch="[master]"
[sf@zeus:~] echo $branch
m

Upvotes: 1

unwind
unwind

Reputation: 400109

Looks like file name expansion, do you have a file in the current directory called m?

Upvotes: 2

Related Questions