36

I saw this in the end of an awesome shell script but I can't understand the logic here because I think it's being short-handed for a longer command.

spark ${@:-`cat`}

This apears at the end of this script. Any ideas?

+ Marks for some one who extends it into a full segment of code, even if its slower (Better for explanation)

jlliagre
  • 61,204
whoami
  • 3,870
  • The question which already has answer doesn't have explanation for @ which is what this question is about. This is definitely not duplicate question – Atul Dec 05 '19 at 11:24

2 Answers2

39

It's the first special case of parameter substitution in man bash:

${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

In the case you mentioned, either the user has provided arguments on the command line and they will be used, or the user is asked to input them on standard input afterwards.

5

Afaik :- is basically: use $@ OR `cat` ( without setting $@ to `cat` )...

so some variable substition thingy...

Edit: So it gives you some sort of interactive input...

Here's a little Terminal test:

localhost:~ _druu$ echo ${@:-`cat`}
hello
world
;)
^\hello world ;)
localhost:~ _druu$