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}

| improve this question | | | | |

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).

| improve this answer | | | | |
  • Thanks, I fixed the typos, it was kind of a thoughtless example for the sake of the question. – Gregg Leventhal Feb 11 '16 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.