0

I have been trying to learn more about bash scripting and came across this statement at the beginning of an installation script I was looking through.

DIR=${1-${HOME}}

Firstly, as this is at the start of the script, I don't understand what $1 could be pointing to. I understand $0 is the script itself. What would $1 refer to?

Secondly, what does it mean to subtract the home directory from $1?

If someone could please explain what is happening here, I'd really appreciate it!

Thanks!

(Btw, the actual script I'm referring to is the installation script for the yin-yang dark theme manager: https://github.com/oskarsh/Yin-Yang/blob/master/install.sh, if that helps any.)

1 Answers1

4

$1, $2, ... are technically called the "positional parameters", they hold the command line arguments to the script (or arguments to a function within the script).

${var-value} is the default value expansion, it uses the value of $var if it's set, otherwise it uses the value given after the -.

So ${1-${HOME}} (or ${1-$HOME}) uses the first command line argument to the script, or if there are no arguments, whatever the value of $HOME is. And the assignment just assigns that result to DIR.

See e.g. Using "${a:-b}" for variable assignment in scripts for examples and the variant with :-.

ilkkachu
  • 138,973