-1

im trying to learn bash right now, came across a script and im not 100% sure if i read it right.

source_dir_123=${SOURCE_DIR:-/tmp}
echo source_dir_123=$source_dir_123

What is happening here? I guess this is some kinda variable assigment, but it looks weird to me. What type of assigment/operation happens here? Any specific name of these types of assignments? Sorry for a newbish question, but i dont get it why would you use these kinda of assignments instead of something more straight forward like

source_dir_12="/tmp"
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
CobraKai
  • 87
  • 1
  • 6
  • 1
    `echo source_dir_123=$source_dir_123` simply outputs `source_dir_123=` followed by whatever is stored in that variable. If it did not already have a value, then `/tmp` will be stored there, thus the full output would be `source_dir_123=/tmp`. – David C. Rankin Oct 31 '19 at 09:44

1 Answers1

2

/tmp is the default value for source_dir_123 in case SOURCE_DIR is not set, then you're displaying the result to the console.

See the following example:

> echo $SOURCE_DIR

> source_dir_123=${SOURCE_DIR:-/tmp}
> echo source_dir_123=$source_dir_123
source_dir_123=/tmp
# now let's set SOURCE_DIR
> SOURCE_DIR=/test
> source_dir_123=${SOURCE_DIR:-/tmp}
> echo source_dir_123=$source_dir_123
source_dir_123=/test
Maroun
  • 94,125
  • 30
  • 188
  • 241