There's a short section in my .bashrc file that I don't understand and I'd like to ask what it means and some syntax explanation.

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases

I understand of course that # makes every line a comment.

Can you please explain to me the if ... then statement. What -f means?

Why there are [] in the first line of the code?

Why there's ; after the [ -f ~/.bash_aliases ]?

Why the second line of the code starts with a . and what it actually means, this whole second line. I know that .bash_aliases is a file containing bash_aliases in Linux, but this second line I don't understand.

  • 2
  • 1
    Does this answer your question? What does -f mean in an if statement in a bash script?
    – OrangeDog
    Mar 26, 2022 at 12:38
  • It almost feels like we have duplicate comments suggesting that this question is a duplicate? Can that be right? On a more serious note, that question’s answer does not explain the ., so it is not a duplicate. Mar 26, 2022 at 12:51
  • 1
    @Quasímodo I assume that before doing any of that, we should head to the proposed duplicate and cast some downvotes and close votes there. It currently has a score of 9 and two answers. One answer is dodgy, but even that answer has a score of 10. There seem to be a lot of inconsistencies here. Mar 27, 2022 at 6:31

1 Answer 1

  1. Can you please explain to me the if ... then statement.

    In plain English: If there is a file named .bash_aliases in your home directory, source it.

  2. What -f means?:

    Test if a file exists and is a regular file (not a directory etc.), or a symbolic link to a regular file.

  3. Why there are [] in the first line of the code?

    That's the shell command for performing a test. An alternative way is if test -f .bash_aliases; then

  4. Why there's ; after the [ -f ~/.bash_aliases ]?

    The semicolon is a command separator in bash. You need it here to end the test command and allow the then block to start on the same line.

    Alternatively, you can avoid using the ; as a separator and explicitly split the commands using a line break.

    if [ -f ~/.bash_aliases ]
  5. Why the second line of the code starts with a . and what it actually means

    That's the name of a command to execute. The . (dot) command is a shell builtin command that sources a file. With the reasonable assumption that bash will execute the file .bashrc, the more readable but non-POSIX command source may be used instead of .

    source ~/.bash_aliases

Be aware that some shells, for example dash and some variants of ksh, do not support source though.

You might want to read bash documentation.

  • 2
    [ is a program. You will find it in /bin/[. Some shells may optimise by handling it internally instead of calling the actual executable.
    – OrangeDog
    Mar 26, 2022 at 12:36
  • @OrangeDog There is no guarantee that the shell’s built-in [ will behave the same way as the external command in /bin/[. Indeed, my Debian 10 system’s man page for [ says: “Please refer to your shell's documentation for details about the options it supports.” Mar 26, 2022 at 12:55
  • @BrianDrake they are guaranteed to behave in exactly the same way. Especially if the docs for one refer you to the other.
    – OrangeDog
    Mar 26, 2022 at 19:16
  • @OrangeDog Although an identical behavior is expected in most cases, there is not guarantee that both always behave the same way. For example errors message might vary.
    – jlliagre
    Mar 26, 2022 at 21:51
  • 1
    @OrangeDog The [ man page I cited gives a complete description of the external [ command, before stating that the shell may have its own version and referring the user to the shell’s documentation. The bash man page also has a complete description of bash’s implementation. A cursory look suggests that there are at least two differences: only bash’s implementation supports -a as a synonym for -e, and only the external command supports -l for the length of a string. Mar 27, 2022 at 6:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .