In addition to other more general answers (especially for Jonathan comment and Kevin a more general answer [which also supports strings]) I would like to add the following two solutions:
setting the variable to either 0 or 1 based on the condition:
(as the example question shows).
General form will read
(condition); variable=$?;
where $variable causes either 0 or 1 and condition can be any valid conditional expression .
eg. variable check ...
[[ $variableToCheck == "$othervariable, string or number to match" ]] variable=$?
... or checking file existence ...
[ -f "$filepath" ] fileExists=$?
... or to check the numeric value of $myNumber :
(( myNumber >= 1000000000 )) is_huge_number=$?
The advantages of this solution are that
- it supports arbitrary conditional expressions , including strings
(which are not supported in Jonathan arithmetic expressions - that
variable declared anyway, unlike giffon answer :
[ -z "$variable" ] && variable="defaultValue"
What would be the question if you want nameref later (for example, from inside a function).
Note: In Bash, is the special variable $? always contains the exit code of a previously executed statement (or statement block, see man bash for more details). Thus, a positive result is usually represented by a value of 0 rather than 1 (see my comment below , thanks to Assimilater for pointing this out ).
Thus, if the condition is true (for example, [[2 eq 2]] ), then $?=0 . If instead you need 0 or 1 in your variable (for example, to print or perform mathematical calculations), then you need to use the solution Jonathan variable=$(( 1 == 1 ? 1 : 0 )) , or - if you want to avoid creating subshells or having arbitrary conditions is the next solution.
setting a variable to arbitrary values:
as is done in most other answers, it can be adapted as follows:
(condition) \ && variable=true \ || variable=false
for example, as in
[[ $variableToCheck == "$othervariable, string or number to match" ]] \ && variable="$valueIfTrue" \ || variable="$valueIfFalse"
or get 1 in a positive test and 0 if it fails (for example, in the example question):
[[ $variableToCheck == "$othervariable, string or number to match" ]] \ && variable=1 \ || variable=0
The advantages of this solution are that
- it can be considered a little better read than Kevin answer
myvar="default" && [[ <some_condition_is_true> ]] && myvar="non-default" and $valueIfTrue conditionally evaluated only if necessary , which will make a difference if you do something- with side effect like
variable=$((i++)) , or{ variable=$1; shift; }
- high computing for example
variable=$(find / -type f -name ListOfFilesWithThisNameOnMySystem)
- slightly shorter than ghostdog74 answer (which, however, is great if you have several conditions!)
- does not open subshell, as in Pierre answer
- and as stated above:
- it supports arbitrary conditional expressions , including strings
(which are not supported in Jonathan arithmetic expressions - that
variable declared anyway, unlike giffon answer :
[ -z "$variable" ] && variable="defaultValue"
What would be the question if you want nameref later (for example, from inside a function).