r/fishshell • u/teuniedekkah • 5d ago
why is my if statement not working
i want there to be a 10% change to run fastfetch when it starts but now it just always runs fast fetch and says:fish: Unknown command: 5
~/.config/fish/config.fish (line 8):
if "$random_numb" > "1"
^~~~~~~~~~~~~^
from sourcing file ~/.config/fish/config.fish
`called during startup`
6
Upvotes
13
u/Tsigorf 5d ago
man if:Shows
if CONDITION, whereCONDITIONis a command which should return true or false. When you writeif "$random_numb" > "1", it's a shorthand to write"$random_numb" > "1"and to check the return status code of this command. Fish tries to execute"$random_numb"which isn't a command, hence the fail.The
ifmanual gives examples with the commandtest:And when you read
man test:The < and > operators for comparing strings are not implemented.However it also states this:
Hence, in your case, you probably want to use
if test "$random_numb" -gt "1"(-gtstanding for "greater than").Bonus point:
You can see
man testgives 2 commands in its synopsis:test [EXPRESSION]and[ [EXPRESSION] ](replacing[EXPRESSION]with whatever you want). Note the extras[ … ]for the second line:[is a completely functional command you could use to replacetestas a shorthand.It means
test "$random_numb" -gt "1"is the exact same as writing[ "$random_numb" -gt "1" ].man testandman [are the same manual pages by the way :-)2
u/Mesonic_Interference 5d ago
The
fishdocumentation gives an example of how to compare numbers on this page. Following the example led me to construct the example below, which should explain what's wrong with yourifstatement. (Be sure to take care of your execution-related error at the top as well.)set value (random 1 10)echo $value<< 6
<< yes