Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ternary conditional operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Bash=== A true ternary operator only exists for arithmetic expressions: <syntaxhighlight lang="bash"> ((result = condition ? value_if_true : value_if_false)) </syntaxhighlight> For strings there only exist workarounds, like e.g.: <syntaxhighlight lang="bash"> result=$([[ "$a" = "$b" ]] && echo "value_if_true" || echo "value_if_false") </syntaxhighlight> Where {{code|1="$a" = "$b"}} can be any condition {{code|[[ β¦ ]]}} construct can evaluate. Instead of the {{code|[[ β¦ ]]}} there can be any other bash command. When it exits with success, the first echo command is executed, otherwise the second one is executed.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)