I am looking for a way to add some script that Bash would source automatically when I start a new shell. Unfortunately, neither /etc/env.d or /etc/profile.d has user-based. So I turned my eyes to ~/.bash_completion.d since it’s quite common to have it installed and enabled.
The reason behind is this command:
$ last -x | head -20 | tac | grep system
shutdown system down 3.10.17-gentoo-1 Wed Nov 6 01:28 - 09:21 (07:53)
reboot system boot 3.10.17-gentoo-1 Wed Nov 6 09:21 - 00:39 (15:17)
shutdown system down 3.10.17-gentoo-1 Thu Nov 7 00:39 - 08:49 (08:10)
reboot system boot 3.10.17-gentoo-1 Thu Nov 7 08:49 - 01:02 (16:12)
shutdown system down 3.10.17-gentoo-1 Fri Nov 8 01:02 - 10:18 (09:16)
reboot system boot 3.10.17-gentoo-1 Fri Nov 8 10:18 - 01:06 (14:47)
shutdown system down 3.10.17-gentoo-1 Sat Nov 9 01:06 - 10:51 (09:45)
reboot system boot 3.10.17-gentoo-1 Sat Nov 9 10:51 - 13:31 (02:39)
I learned last command six months ago and had been using Bash’s history reverse search Ctrl+R to grab the entire command to run it. Since I relied on history file, eventually, one way or another, someday the command would fall out of history file and that did already happen once.
This command I use very often to check up times, but I don’t feel like adding it to my .bashrc, because it’s not really that important and I don’t want to create additional commit, my dotfiles are revision controlled.
By using ~/.bash_completion.d, although it’s not made for what I need, it does the job well. I added a simple file with an alias.
You can actually use completion for keeping or adding entry in ~/.bash_history, for example:
if ! grep -q "$PATTERN" "$HISTFILE"; then
echo "$THE_COMMAND" >> "$HISTFILE"
history -n # or -r
fi
With that, you should able to look up the command using Ctrl+R, the entry will always be in the history file. At least, in the beginning of ever shell invoked.
Note that if you need to use whatever stuff you put in those files, you need to invoke Bash with -li or interactive login shell. That is
#!/bin/bash -li
the_command_or_stuff_in_completion_script
for a shell script file or
$ bash -lic 'the_command_or_stuff_in_completion_script'
directly in shell prompt.
Now, I think I will use ~/.bash_completion.d for more than just completion, it has much more potentials than just being hit with Tab by the users.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.