.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "IF" "1" "Nov 13, 2025" "4.2" "fish-shell" .SH NAME if \- conditionally execute a command .SH SYNOPSIS .nf \fBif\fP \fICONDITION\fP\fB;\fP \fICOMMANDS_TRUE\fP \&...\fB;\fP [\fBelse\fP \fBif\fP \fICONDITION2\fP\fB;\fP \fICOMMANDS_TRUE2\fP \&...\fB;\fP] [\fBelse\fP\fB;\fP \fICOMMANDS_FALSE\fP \&...\fB;\fP] \fBend\fP .fi .sp .SH DESCRIPTION .sp \fBif\fP will execute the command \fBCONDITION\fP\&. If the condition\(aqs exit status is 0, the commands \fBCOMMANDS_TRUE\fP will execute. If the exit status is not 0 and \fI\%else\fP is given, \fBCOMMANDS_FALSE\fP will be executed. .sp You can use \fI\%and\fP or \fI\%or\fP in the condition. See the second example below. .sp The exit status of the last foreground command to exit can always be accessed using the \fI\%$status\fP variable. .sp The \fB\-h\fP or \fB\-\-help\fP option displays help about using this command. .SH EXAMPLE .sp The following code will print \fBfoo.txt exists\fP if the file foo.txt exists and is a regular file, otherwise it will print \fBbar.txt exists\fP if the file bar.txt exists and is a regular file, otherwise it will print \fBfoo.txt and bar.txt do not exist\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .EX if test \-f foo.txt echo foo.txt exists else if test \-f bar.txt echo bar.txt exists else echo foo.txt and bar.txt do not exist end .EE .UNINDENT .UNINDENT .sp The following code will print \(dqfoo.txt exists and is readable\(dq if foo.txt is a regular file and readable .INDENT 0.0 .INDENT 3.5 .sp .EX if test \-f foo.txt and test \-r foo.txt echo \(dqfoo.txt exists and is readable\(dq end .EE .UNINDENT .UNINDENT .SH SEE ALSO .sp \fBif\fP is only as useful as the command used as the condition. .sp Fish ships a few: .INDENT 0.0 .IP \(bu 2 \fI\%test \- perform tests on files and text\fP can compare numbers, strings and check paths .IP \(bu 2 \fI\%string \- manipulate strings\fP can perform string operations including wildcard and regular expression matches .IP \(bu 2 \fI\%path \- manipulate and check paths\fP can check paths for permissions, existence or type .IP \(bu 2 \fI\%contains \- test if a word is present in a list\fP can check if an element is in a list .UNINDENT .SH COPYRIGHT fish-shell developers .\" Generated by docutils manpage writer. .