5

I'm using a central location for my bashrc and source that for all my users like this:

GLOBAL_BASHRC="/path/to/global/bashrc"

if [ -r "$GLOBAL_BASHRC" -a -f "$GLOBAL_BASHRC" ]; then
    # Load the bashrc
    source $GLOBAL_BASHRC
else
    echo "Warning: Bashrc file [${GLOBAL_BASHRC}] does not exist!"
fi

And now I want to do the same thing for my inputrc file. I already found out how to do the equivalent to source in inputrc from this question:

$include /path/to/global/inputrc

But the thing is, like in the bashrc above I want some error handling, I only want to load the file if it actually exists. Therefore my question is, how do I specify an if statement checking if a file exists in my inputrc?

yzfr1
  • 207

2 Answers2

4

You don't need to $include from your ~/.inputrc as you can read an inputrc file at any time with

bind -f /path/to/global/inputrc

so use your usual if [ -r file ] with this instead of source.


The man page for says for an interactive login shell it reads /etc/profile and the first found of ~/.bash_profile, ~/.bash_login, and ~/.profile. For other interactive shells it reads ~/.bashrc, and for non-interactive it reads file $BASH_ENV if any.

In your case presumably you are using a non-login interactive shell for a terminal emulator, so ~/.bashrc is read. You can see what happens using strace and a dummy home, eg in /tmp.

$ touch /tmp/.inputrc /tmp/.bashrc
$ (unset BASH_ENV INPUTRC HISTFILE
   HOME=/tmp strace -e open -o /tmp/out bash -i)

This shows the following files being opened (I've removed some for brevity):

open("/tmp/.bashrc", O_RDONLY)          = 3
open("/tmp/.bash_history", O_RDONLY)    = -1 ENOENT
open("/tmp/.inputrc", O_RDONLY)         = 3
open("/tmp/.bash_history", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 3

So bash will read .inputrc after .bashrc. This makes sense as it gives you time to set INPUTRC in your file.

meuh
  • 51,383
2

Rather than doing this in the .inputrc file (which has no facility for checking that the file actually exists AFAIK), you may set the INPUTRC environment variable in your .bashrc file:

if [ -r "$GLOBAL_BASHRC" -a -f "$GLOBAL_BASHRC" ]; then
    # Load the bashrc
    source $GLOBAL_BASHRC
    if [ -f /path/to/global/inputrc ]; then
        export INPUTRC="/path/to/global/inputrc"
    fi
else
    echo "Warning: Bashrc file [${GLOBAL_BASHRC}] does not exist!"
fi

The INPUTRC variable is documented in the readline manual (and in the bash manual too).

Kusalananda
  • 333,661
  • Yes, I am aware that I can change which INPUTRC file is used, but I want to load the global one AND set some local settings specific to the account / machine / whatever. So I'm adding the include as the first line in my inputrc and then add some other settings after that. So if I understand your answer right this is not possible? – yzfr1 Jun 21 '16 at 13:56
  • 1
    @yzfr1 As far as I can work out, it is not possible to check in the .inputrc whether another file exists and then make a decision based on that test, no. – Kusalananda Jun 21 '16 at 14:07