Is there any way to accomplish the following in one stroke using Bash?

my_var=${$(grep -E '^setting_1' /settings.conf):-default_setting1}

As opposed to:

my_var=$(grep -E '^setting_1' /settings.conf); my_var=${my_var:-default_setting}

1 Answer 1


You can output the default if the grep fails:

var=$( grep -e '^setting_1' || echo default_setting )

Note that you didn't close the quotes (and without the file argument, grep will filter stdin).

  • Thanks, I fixed the typos, it was kind of a thoughtless example for the sake of the question. Feb 11, 2016 at 17:18

You must log in to answer this question.

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