Skip to main content
avoiding typo confusion
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265

I know that in Bash you can use the syntax

editor = $editor=${new_editor:-/usr/bin/vi}

which will set editor to new_editor if the variable new_editor is not empty and to /usr/bin/vi otherwise. Is it possible to produce similar one-liner, which will set editor to output of some command and to some default value if the output is empty? Something like (only as example, this does not work)

editor=$( $(which emacs) :-/usr/bin/vi )

I know how to do it in several lines of code, but would like to know if an elegant solution exists.

I know that in Bash you can use the syntax

editor = ${new_editor:-/usr/bin/vi}

which will set editor to new_editor if the variable new_editor is not empty and to /usr/bin/vi otherwise. Is it possible to produce similar one-liner, which will set editor to output of some command and to some default value if the output is empty? Something like (only as example, this does not work)

editor=$( $(which emacs) :-/usr/bin/vi )

I know how to do it in several lines of code, but would like to know if an elegant solution exists.

I know that in Bash you can use the syntax

editor=${new_editor:-/usr/bin/vi}

which will set editor to new_editor if the variable new_editor is not empty and to /usr/bin/vi otherwise. Is it possible to produce similar one-liner, which will set editor to output of some command and to some default value if the output is empty? Something like (only as example, this does not work)

editor=$( $(which emacs) :-/usr/bin/vi )

I know how to do it in several lines of code, but would like to know if an elegant solution exists.

edited body
Source Link
anon
anon

I know that in Bash you can use the syntax

editor = ${new_editor:-/usr/bin/vi}

which will set editor to new_editor if the variable new_editor is not empty and to /usr/bin/vi otherwise. Is it possible to produce similar one-liner, which will set editor to output of some command and to some default value if the output is empty? Something like (only as example, this does not work)

editor=$( $(which emacs) :-/usr/bin/vi )

I know how to do it in several lines of code, but would like to know if an elegangelegant solution exists.

I know that in Bash you can use the syntax

editor = ${new_editor:-/usr/bin/vi}

which will set editor to new_editor if the variable new_editor is not empty and to /usr/bin/vi otherwise. Is it possible to produce similar one-liner, which will set editor to output of some command and to some default value if the output is empty? Something like (only as example, this does not work)

editor=$( $(which emacs) :-/usr/bin/vi )

I know how to do it in several lines of code, but would like to know if an elegang solution exists.

I know that in Bash you can use the syntax

editor = ${new_editor:-/usr/bin/vi}

which will set editor to new_editor if the variable new_editor is not empty and to /usr/bin/vi otherwise. Is it possible to produce similar one-liner, which will set editor to output of some command and to some default value if the output is empty? Something like (only as example, this does not work)

editor=$( $(which emacs) :-/usr/bin/vi )

I know how to do it in several lines of code, but would like to know if an elegant solution exists.

Source Link
John Smith
  • 2.9k
  • 1
  • 16
  • 19

bash one-liner: set variable to output of command or to default value if output is empty

I know that in Bash you can use the syntax

editor = ${new_editor:-/usr/bin/vi}

which will set editor to new_editor if the variable new_editor is not empty and to /usr/bin/vi otherwise. Is it possible to produce similar one-liner, which will set editor to output of some command and to some default value if the output is empty? Something like (only as example, this does not work)

editor=$( $(which emacs) :-/usr/bin/vi )

I know how to do it in several lines of code, but would like to know if an elegang solution exists.