What does -f mean in an if statement in a bash script?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Trying to understand this piece of code:
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
I'm not sure what the -f means exactly.
linux shell-script
add a comment |Â
up vote
2
down vote
favorite
Trying to understand this piece of code:
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
I'm not sure what the -f means exactly.
linux shell-script
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Trying to understand this piece of code:
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
I'm not sure what the -f means exactly.
linux shell-script
Trying to understand this piece of code:
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
I'm not sure what the -f means exactly.
linux shell-script
asked Jul 4 at 21:56
Eric Hodgins
192
192
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
down vote
accepted
The relevant man page to check for this is that of the shell itself, bash
, because -f
is functionality that the shell provides, it's a bash built-in.
On my system (CentOS 7), the fine man page covers it. The grep
may not give the same results on other distributions. Nevertheless, if you run man bash
and then search for '-f' it should give the results you require.
$ man bash | grep -A1 '-f file$'
-f file
True if file exists and is a regular file.
$
add a comment |Â
up vote
7
down vote
In short, the piece of code will source /etc/bashrc
file if it exists, and the existence is verified by [
command to which -f
is an operator/parameter.
if...then...else...fi
statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:
if ping -c 4 google.com; then
echo "We have a connection!"
fi
The command, in your case, is [
which is also known as test
command. So it'd be perfectly valid to do
if test -f /etc/bashrc; then
. /etc/bashrc
fi
The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc
is in fact a directory or missing, test should return non-zero exit status to signal failure
This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.
On a side note, the /etc/bashrc
seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc
, which is intended as system-wide rc-file for bash, so one would expect that to be used.
See also:
- How to conditionally do something if a command succeeded or failed
- How exactly does âÂÂ/bin/[â work?
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
The relevant man page to check for this is that of the shell itself, bash
, because -f
is functionality that the shell provides, it's a bash built-in.
On my system (CentOS 7), the fine man page covers it. The grep
may not give the same results on other distributions. Nevertheless, if you run man bash
and then search for '-f' it should give the results you require.
$ man bash | grep -A1 '-f file$'
-f file
True if file exists and is a regular file.
$
add a comment |Â
up vote
6
down vote
accepted
The relevant man page to check for this is that of the shell itself, bash
, because -f
is functionality that the shell provides, it's a bash built-in.
On my system (CentOS 7), the fine man page covers it. The grep
may not give the same results on other distributions. Nevertheless, if you run man bash
and then search for '-f' it should give the results you require.
$ man bash | grep -A1 '-f file$'
-f file
True if file exists and is a regular file.
$
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
The relevant man page to check for this is that of the shell itself, bash
, because -f
is functionality that the shell provides, it's a bash built-in.
On my system (CentOS 7), the fine man page covers it. The grep
may not give the same results on other distributions. Nevertheless, if you run man bash
and then search for '-f' it should give the results you require.
$ man bash | grep -A1 '-f file$'
-f file
True if file exists and is a regular file.
$
The relevant man page to check for this is that of the shell itself, bash
, because -f
is functionality that the shell provides, it's a bash built-in.
On my system (CentOS 7), the fine man page covers it. The grep
may not give the same results on other distributions. Nevertheless, if you run man bash
and then search for '-f' it should give the results you require.
$ man bash | grep -A1 '-f file$'
-f file
True if file exists and is a regular file.
$
edited Jul 6 at 13:03
answered Jul 4 at 22:11
steve
12k22047
12k22047
add a comment |Â
add a comment |Â
up vote
7
down vote
In short, the piece of code will source /etc/bashrc
file if it exists, and the existence is verified by [
command to which -f
is an operator/parameter.
if...then...else...fi
statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:
if ping -c 4 google.com; then
echo "We have a connection!"
fi
The command, in your case, is [
which is also known as test
command. So it'd be perfectly valid to do
if test -f /etc/bashrc; then
. /etc/bashrc
fi
The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc
is in fact a directory or missing, test should return non-zero exit status to signal failure
This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.
On a side note, the /etc/bashrc
seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc
, which is intended as system-wide rc-file for bash, so one would expect that to be used.
See also:
- How to conditionally do something if a command succeeded or failed
- How exactly does âÂÂ/bin/[â work?
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
add a comment |Â
up vote
7
down vote
In short, the piece of code will source /etc/bashrc
file if it exists, and the existence is verified by [
command to which -f
is an operator/parameter.
if...then...else...fi
statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:
if ping -c 4 google.com; then
echo "We have a connection!"
fi
The command, in your case, is [
which is also known as test
command. So it'd be perfectly valid to do
if test -f /etc/bashrc; then
. /etc/bashrc
fi
The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc
is in fact a directory or missing, test should return non-zero exit status to signal failure
This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.
On a side note, the /etc/bashrc
seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc
, which is intended as system-wide rc-file for bash, so one would expect that to be used.
See also:
- How to conditionally do something if a command succeeded or failed
- How exactly does âÂÂ/bin/[â work?
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
add a comment |Â
up vote
7
down vote
up vote
7
down vote
In short, the piece of code will source /etc/bashrc
file if it exists, and the existence is verified by [
command to which -f
is an operator/parameter.
if...then...else...fi
statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:
if ping -c 4 google.com; then
echo "We have a connection!"
fi
The command, in your case, is [
which is also known as test
command. So it'd be perfectly valid to do
if test -f /etc/bashrc; then
. /etc/bashrc
fi
The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc
is in fact a directory or missing, test should return non-zero exit status to signal failure
This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.
On a side note, the /etc/bashrc
seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc
, which is intended as system-wide rc-file for bash, so one would expect that to be used.
See also:
- How to conditionally do something if a command succeeded or failed
- How exactly does âÂÂ/bin/[â work?
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
In short, the piece of code will source /etc/bashrc
file if it exists, and the existence is verified by [
command to which -f
is an operator/parameter.
if...then...else...fi
statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:
if ping -c 4 google.com; then
echo "We have a connection!"
fi
The command, in your case, is [
which is also known as test
command. So it'd be perfectly valid to do
if test -f /etc/bashrc; then
. /etc/bashrc
fi
The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc
is in fact a directory or missing, test should return non-zero exit status to signal failure
This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.
On a side note, the /etc/bashrc
seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc
, which is intended as system-wide rc-file for bash, so one would expect that to be used.
See also:
- How to conditionally do something if a command succeeded or failed
- How exactly does âÂÂ/bin/[â work?
- What is the difference between the Bash operators [[ vs [ vs ( vs ((?
edited Jul 5 at 8:49
answered Jul 4 at 23:18
Sergiy Kolodyazhnyy
7,53811545
7,53811545
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453502%2fwhat-does-f-mean-in-an-if-statement-in-a-bash-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password