7

There is nothing about command history in man bluetoothctl, info bluetoothctl, and bluetoothctl --help.

11

Short answer

bluetoothctl stores command history in ~/.cache/.bluetoothctl_history.


Long answer

Disclaimer: long answer requires some understanding of programming language C.

bluetoothctl is a command line tool which is shipped with BlueZ – Bluetooth protocol stack for Linux. If we look at the source code of BlueZ:

we'll quickly realize that bluetoothctl is using the GNU Readline library for its interactive shell. Per Readline's documentation, function write_history can be used to write history into a file. If we just grep for name of this function in the BlueZ source code:

$ grep write_history -r
src/shared/shell.c:             write_history(data.history);

History of the commands in bluetoothctl is written into a file, whose name is held in the field .history of the struct data. Then, just grepping for accesses to this field, we'll find where it is initialized:

static void rl_init_history(void)
{
        const char *name;
        char *dir;

        memset(data.history, 0, sizeof(data.history));

        name = strrchr(data.name, '/');
        if (!name)
                name = data.name;
        else
                name++;

        dir = getenv("XDG_CACHE_HOME");
        if (dir) {
                snprintf(data.history, sizeof(data.history), "%s/.%s_history",
                                                        dir, name);
                goto done;
        }

        dir = getenv("HOME");
        if (dir) {
                snprintf(data.history, sizeof(data.history),
                                "%s/.cache/.%s_history", dir, name);
                goto done;
        }

        dir = getenv("PWD");
        if (dir) {
                snprintf(data.history, sizeof(data.history), "%s/.%s_history",
                                                        dir, name);
                goto done;
        }

        return;

done:
        read_history(data.history);
        using_history();
        bt_shell_set_env("HISTORY", data.history);
}

Here, XDG_CACHE_HOME comes from freedesktop.org spec. The other environment variables are basic $HOME and $PWD. Field data.name is set up elsewhere:

void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
{
...
        data.name = strrchr(argv[0], '/');
...
}

So variable char *name in the function rl_init_history will contain the string bluetoothctl, name of the executable file. See description of argv in C.

So in most desktop environments, which follow the freedesktop.org spec, command line tool bluetoothctl will store the history of commands in the file ~/.cache/.bluetoothctl_history. If the environment variable XDG_CACHE_HOME is defined, then history of commands will be stored in $XDG_CACHE_HOME/.bluetoothctl_history.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.