I have a handy bash function called calc
:
calc () {
bc -l <<< "$@"
}
Example usage:
$ calc 65320/670
97.49253731343283582089
$ calc 65320*670
43764400
You can change this to suit yourself. For example:
divide() {
bc -l <<< "$1/$2"
}
Note: <<<
is a here string which is fed into the stdin of bc
. You don't need to invoke echo
.
This is not as comprehensive as real accounting, and it can be undone easily by a user, but assuming it doesn't have to be a real accounting system and that both BASH and rsyslog are in use, edit the system-wide BASH RC file:
sudo -e /etc/bash.bashrc
Append to the end of that file:
export PROMPT_COMMAND='RET_VAL=$?;logger -p local6.debug "$(whoami) [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RET_VAL]"'
Above, logger
records the time, user, command, and command return value. Set up logging for "local6" with a new file:
sudo -e /etc/rsyslog.d/bash.conf
And the contents...
local6.* /var/log/bash_commands.log
Restart rsyslog:
sudo service rsyslog restart
Log out. Log in. Log rotation:
sudo -e /etc/logrotate.d/rsyslog
There is a list of log files to rotate the same way...
/var/log/mail.warn
/var/log/mail.err
[...]
/var/log/message
Add the new bash-commands log file in that list:
/var/log/bash_commands.log
Save and restart/reload rsyslog. (The rotation will eventually overwrite log files, so more thought or configuration may be needed.)
To see all the files that a package has installed:
dpkg-query -L [package_name]
So you could execute something like that when you install software.
dpkg-query -L abc-package > /var/log/files_abc-package.log
Best Answer
Shortcut Alt-c (bash)
With bash, using the readline utility, we can define a key sequence to place the word
calc
at the start and enclose the text written so far into double quotes:Having executed that, you type
23 + 46 * 89
for example, then Alt-c to get:Just press enter and the math will be executed by the function defined as calc, which could be as simple as, or a lot more complex:
a (+) Alias
We can define an alias:
Which will comment the whole command line typed so far. You type:
When you press enter, the line will be converted to
calc #(56 * 23 + 26) / 17
and the commandcalc
will be called. If calc is this function:bash
ksh
zsh zsh doesn't allow neither a
+
alias nor a#
character.The value will be printed as:
Only a
+
is required, String is quoted (no globs), shell variables accepted:a (+) Function
With some limitations, this is the closest I got to your request with a function (in bash):
Which will work like this:
The problem is that the shell parsing isn't avoided and a
*
(for example) could get expanded to the list of files in the pwd.If you write the command line without (white) spaces you will probably be ok.
Beware of writing things like
$(...)
because they will get expanded.The safe solution is to quote the string to be evaluated:
Which is only two characters shorter that your
_bc "6/2"
, but a+
seems more intuitive to me.