Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}

share|improve this question
up vote 0 down vote accepted

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

share|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 at 17:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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