5

In ~/.bashrc I have

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Instead why can't it be

PS1='${debian_chroot}\u@\h:\w\$ '

It would accomplish the same thing. Wouldn't it?

1

1 Answer 1

6

It's not the same, note the parenthesis:

$ var=foo
$ echo "${var} ${var:+($var)}"
foo (foo)

The :+ expansion is there so that those aren't printed if the var is empty.

3
  • I accept your answer. The reason I couldn't see it myself was that I had the pre-conceived notion that the parenthesis were going to disappear as a result of some Bash expansion. May 23, 2020 at 20:48
  • @SamehRamzyLabib, yeah. It looks a bit like command substitution $(...) here, but I don't think the parens are special in any ${...} expansion. (I might forget something.) Zsh does use them for glob qualifiers etc., though.
    – ilkkachu
    May 23, 2020 at 20:53
  • @SamehRamzyLabib if you accept the answer, then please formally do so by clicking the checkmark underneat the up/down arrows next to this answer. It's the way it works here and also gives a reward (in reputation) to the person having given the answer. May 23, 2020 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .