0

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.

1
  • I can not reproduce this behavior. Please provide more info on the bash version, OS etc Commented Apr 17, 2012 at 13:31

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

@ScottFrazer: Compare with echo * or touch a b c; echo [abcdefghijklmnopqrstuvwxyz] - It lists files which match the expression. If you want to avoid expansion, Use More Quotes.
1

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

Comments

1

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.