5

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 fi

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.

1 Answers1

15
  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 ]
    then
    
  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.

Kusalananda
  • 333,661
jlliagre
  • 61,204
  • 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 '22 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.” – Brian Drake Mar 26 '22 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 '22 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 '22 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. – Brian Drake Mar 27 '22 at 06:41