List of 'if' switches anywhere?
list.of java
category name list
list of s
wikipedia categories list
list of p
list of lists to make
list of lists python
Is there a list of all the if
switches for use in bash scripting? Sometimes I see someone using it and I wonder what the switch they're using actually does.
Example is the -z
in this one. I know how to use it, but I don't know where it was derived from.
if [ -z "$BASH_VERSION" ]; then echo -e "Error: this script requires the BASH shell!" exit 1 fi
Any references, guides, posts, answers would be appreciated.
Look at the bash manpage (man bash
). The options are specified in the CONDITIONAL EXPRESSIONS
section:
CONDITIONAL EXPRESSIONS Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons. Expressions are formed from the following unary or binary primaries. If any file argument to one of the pri- maries is of the form /dev/fd/n, then file descriptor n is checked. If the file argument to one of the primaries is one of /dev/stdin, /dev/stdout, or /dev/stderr, file descriptor 0, 1, or 2, respectively, is checked. Unless otherwise specified, primaries that operate on files follow sym- bolic links and operate on the target of the link, rather than the link itself. -a file True if file exists. ... more options ...
It is also explained in the help:
$ help [ [: [ arg... ] This is a synonym for the "test" builtin, but the last argument must be a literal `]', to match the opening `['.
Category:Lists of lists, If something is wrong with an entry other than simply having slightly outdated statistics, post about it on the talk page. Lists of Wikipedias by various criteria: edit. Define list. list synonyms, list pronunciation, list translation, English dictionary definition of list. n. 1. A series of names, words, or other items written, printed, or imagined one after the other: a shopping list; a guest list; a list of things to d
Yes. These are called conditional expressions and these are used by the [[
compound command and the test
and [
builtin commands ([
is simply a synonym for test
).
Read section 6.4 Bash Conditional Expressions of the Bash Reference Manual, which contains a list of all these switches and their usage.
List of Wikipedias - Meta - Meta-Wiki, List definition is - a simple series of words or numerals (such as the names of persons or objects). How to use list in a sentence. Antigua and Barbuda. Antigua and Barbuda is a Commonwealth realm with 1 autonomous region, Barbuda. Member of the UNASUL. Barbados. Barbados is a Commonwealth realm. Belarus forms the Union State jointly with Russia . Member of the EU. Belize. Belize is a Commonwealth realm. Member of the UNASUL.
They are not switches for the if
statement, but for the test
command ([
is a synonym for the test
builtin). See help test
in bash for a complete list.
List, NIH is made up of 27 Institutes and Centers, each with a specific research agenda, often focusing on particular diseases or body systems. Placenames Tasmania Land Titles Office Office of the Valuer-General Land Tasmania Open Data Spatial Web Services Fruit Fly Information Spatial Discovery Aerial Photography Location Matters TASMAP Frequently Asked Questions Placenames Tasmania Land Titles Office Office of the Valuer-General Land Tasmania Open Data Spatial Web Services Fruit Fly
It's actually not if
that's providing those — it's [
, better known by the name of test
. help test
should give you a list of all options it can take. You could also look at the standard, if you care.
List of NIH Institutes, Centers, and Offices, Based on a recommendation by DHS and its intergovernmental partners, President Trump signed a proclamation on May 24 and amended on Australian Birds: Photos and facts about birds of Australia including habitat, conservation, distribution maps
The single square brackets ([ ... ]
) is an synonym of the test
command. If you look at the manpage for test, you will see almost all (BASH might have a few extra not mentioned here) of the various if switches as you called them. All in one easy to find place.
If you use double square brackets ([[ ... ]]
), you are using an extended BASH set of tests. These mainly have to do with regular expression matching, and glob matching (and extended glob matching if you have that set too). For that, you'll have to read that BASH manpage.
You called them if switches, but that's not really correct. These are tests and really have nothing to do with the if
command.
The if
command merely executes the command you give it, and then if that command returns an exit code of 0
, will run the if
portion of the if
statement. Otherwise, it will run the else
portion (if that's present).
Let's look at this:
$ rm foo.test.txt # Hope this wasn't an important file $ if ls foo.test.txt > then > echo "This file exists" > else > echo "I can't find it anywhere.." > fi ls: foo.test.txt: No such file or directory I can't find it anywhere..
The if
statement runs the ls foo.test.txt
command and the ls
command returns a non-zero because the file does not exist. This causes the if
statement to execute the else
clause.
Let's try that again...
$ touch foo.test.txt # Now this file exists. $ if ls foo.test.txt # Same "if/else" statement as above > then > echo "This file exists" > else > echo "I can't find it anywhere.." > fi foo.test.txt This file exists
Here, the ls
command returned a 0
exit status (since the file exists and the file exists and can be stat'ed by the ls
command.
Normally, you shouldn't use the ls
command to test for a file. I merely used it here to show that the if
statement executes the command, then executed the if
or else
clause depending upon the exit status of that command. If you want to test whether or not a file exists, you should use the test -e
command instead of ls
command:
if test -e foo.test.txt # Same as above, but using "test" instead of "ls" then echo "This file exists" else echo "I can't find it anywhere.." fi
If the file exists, test -e
will return an exit status of 0
. Otherwise, it will return a non-zero exit status.
If you do this:
$ ls -i /bin/test /bin/[ 10958 /bin/[ 10958 /bin/test
That 10958
is the inode. Files with the same inode are two different names for the same file. Thus [
and test
command are soft linked1. This means you can use [
instead of test
:
if [ -e foo.test.txt ] then echo "This file exists" else echo "I can't find it anywhere.." fi
Looks familiar?
1. In BASH, the test
and [
are builtin, so when you run these commands in BASH, it isn't running /bin/test
or /bin/[
. However, they're still linked to each other.
Department of Homeland Security Adds Brazil to List of Countries for , MINNEAPOLIS (WCCO) – On Wednesday, Gov. Tim Walz gave Minnesota restaurants the green light to open their patios starting on June 1, Here are the prime numbers in the range 0 to 10,000. For more see Prime Number Lists.
A List Of Restaurants Reopening For Outdoor Dining – WCCO, This page lists current Emergency Use Authorizations that make available diagnostic and therapeutic medical devices to diagnose and respond to public health Formed in 1990, List A is widely recognised as Australia’s leading list of commercial barristers with a national and international reputation for professional excellence and superior quality in the provision of legal services. Its senior practitioners are the leaders of the Victorian Bar and its members represent the Federal and State
Emergency Use Authorizations, Pandemic bankruptcies: A running list of retailers that have filed for Chapter 11. Tuesday Morning became the fifth major retailer file for *The RSMS ROL list is a separate list of occupations that applies to the RSMS visa only. To respond to changes in the Australian labour market and regional variations across Australia, the list is reviewed regularly by the Department of Jobs and Small Business .
Retail bankruptcies during the coronavirus pandemic: A running list , Workers at New York State Veterans Home in St. Albans, Queens, demand justice for fallen veterans with an act of defiance, honor and The Forbes World’s Billionaires list is a snapshot of wealth using stock prices and exchange rates from March 18, 2020. Some people become richer or poorer within days of publication.
Comments
- gnu.org/software/bash/manual/bashref.html
[
is a short name for thetest
command. Tryman test
.- @Barmar you mean
help test
.man test
is the documentation for/bin/test
, and it supports different options from thetest
builtin - ffs just run
man [
! -z
is is an option to[
ortest
; it has nothing (directly) to do withif
.- Online manual: Bash Conditional Expressions
- Single page: 6.4 Bash Conditional Expressions
- -1: test is both a command
/bin/test
and a bash builtin.man test
gives you the doc for the command, but[
is a synonym for the builtin. As an example of divergent behavior, on OSX the bash builtin supports the-N
test but the/bin/test
does not [
is not an alias of test. synonym is the word the manpage uses. Alias has a specific meaning (in the bash manpage, read the sectionALIASES
)- Okay. I'll change that. I was simply looking for a word that would explain how the two are linked.