2019-03-31 09:05:09 +00:00
.. _cmd-count:
2018-12-17 01:39:33 +00:00
count - count the number of elements of an array
2019-01-03 04:10:47 +00:00
================================================
2018-12-17 01:39:33 +00:00
2018-12-18 01:58:24 +00:00
Synopsis
--------
2018-12-16 21:08:41 +00:00
count $VARIABLE
2018-01-03 23:42:12 +00:00
COMMAND | count
count < FILE
2018-12-18 01:58:24 +00:00
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Description
2019-01-03 04:10:47 +00:00
-----------
2018-12-16 21:08:41 +00:00
2018-01-03 23:42:12 +00:00
`` count `` prints the number of arguments that were passed to it, plus the number of newlines passed to it via stdin. This is usually used to find out how many elements an environment variable array contains, or how many lines there are in a text file.
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` count `` does not accept any options, not even `` -h `` or `` --help `` .
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` count `` exits with a non-zero exit status if no arguments were passed to it, and with zero if at least one argument was passed.
2018-12-16 21:08:41 +00:00
2018-01-03 23:42:12 +00:00
Note that, like `` wc -l `` , reading from stdin counts newlines, so `` echo -n foo | count `` will print 0.
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Example
2019-01-03 04:10:47 +00:00
-------
2018-12-16 21:08:41 +00:00
2018-12-19 03:14:04 +00:00
::
count $PATH
# Returns the number of directories in the users PATH variable.
count *.txt
# Returns the number of files in the current working directory ending with the suffix '.txt'.
2018-01-03 23:42:12 +00:00
git ls-files --others --exclude-standard | count
# Returns the number of untracked files in a git repository
printf '%s\n' foo bar | count baz
# Returns 3 (2 lines from stdin plus 1 argument)
count < /etc/hosts
# Counts the number of entries in the hosts file