I often see scripts with usage of options as in if [ -f some_file ]
checks whether some_file
is a file. I have a very vague understandings of flags, but can someone give me a good explanation? Others like -p
, -s
, I have no clue what they are and was unable to find good resources on them as a single google search doesn't yield any good result.
-
Are you asking about "[]" command? That is basically called test command in Bash. Just lookup the man page of test.– VikybossFeb 14, 2016 at 5:33
-
no, I am asking about options, a character follows by a "-"– 夢のの夢Feb 14, 2016 at 5:39
-
You are asking for the option for test command. When you execute "man test" and look through it, you can see all the options(i.e, -f = FILE exists and is a regular file, -p FILE = exists and is a named pipe, etc.)– VikybossFeb 14, 2016 at 6:19
-
Go to the official documentation gnu.org/software/bash/manual/…– cuonglmFeb 14, 2016 at 7:03
-
basic examples and explanations can also be found here: cyberciti.biz/tips/…– xaaFeb 14, 2016 at 8:21
2 Answers
Believe it or not, [
is actually another name for test
command.
You can check the man
page:
man test
test
is a shell builtin too in most shells (and it gets precedence over the external one):
% type -a \[
[ is a shell builtin
[ is /usr/bin/[
% type -a test
test is a shell builtin
test is /usr/bin/test
In bash
you can also see help test
.
The best solution is the manual for bash, yes the simple man bash
.
To find the exact entry in the manual, execute this command:
LESS=+/'^CONDITIONAL EXPRESSIONS' man bash
When working inside a bash shell, this also helps:
help test