Understand this .bashrc script (curly braces, eval, ...)

It's hard for me to understand what is written in my ubuntu .bashrc , which is shown in the part below. Here is what I do not understand:

  • What is the purpose of curly braces and / + characters used after:? (for example: $ {debian_chroot: -} and $ {debian_chroot: + ($ debian_chroot)})

  • The eval command.

  • How the following code snippet works.

     [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot) fi if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\ u@ \h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\ u@ \h:\w\$ ' fi 
+4
source share
1 answer

${var:-default} means $var if $var is defined and otherwise "default"

${var:+value} means if $var is defined use "value"; otherwise nothing if $var is defined use "value"; otherwise nothing

The second may seem a little strange, but your code snippet shows a typical use:

 ${debian_chroot:+($debian_chroot)} 

This means that "if $ debian_chroot is specified, then insert it in parentheses."

Above, “defined” means “set to some non-zero value”. Unix shells usually do not distinguish between uninstalled variables and variables set to an empty string, but bash may be told to raise the error condition if an undefined variable is used. (You do this with set -u .) In this case, if debian_chroot has never been installed, $debian_chroot will result in an error, and ${debian_chroot:-} will use $debian_chroot if it was installed, otherwise - empty string.

+9
source

Source: https://habr.com/ru/post/1479465/


All Articles