Skip to main content
added 196 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

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:

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

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:

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:

Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

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

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: