Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i just fresh-installed Lion, and i am in the process of seeing up my system. i am using this function (found somewhere online...) to show it in the prompt :

    maxcap=`ioreg -l | grep Capacity | awk '/MaxCapacity/{print $5}'`
    curcap=`ioreg -l | grep Capacity | awk '/CurrentCapacity/{print $5}'`

    curcap=`echo $curcap*1000|bc`
    proc=`echo $curcap / $maxcap |bc`;

    len=`echo ${#proc}`;


    proc_num_len=`echo ${#proc_num}`;

    if [ $proc_num_len == "3" ]; then
            if [ $proc_num != "100" ]; then

    echo $proc_num.$proc_dec%

And i generate my prompt like this :

_PS1time="$txtwht | \t | "
_PS1end="$txtrst\n\w › "

export PS1="$_PS1user$_PS1host$_PS1time$_PS1battery$_PS1end"

unset _PS1user _PS1host _PS1time _PS1battery _PS1end

However, since Lion, the battery Part is not updated every time the prompt appears again. For example, if i do a ls and wait for the battery to drained and then do another command in the same window, without having re source my .bashrc, the battery stays the same. does anyone know how to have it updated without having to resource my .bashrcfile resourced every time ?

(By the way, this is how my prompt looks like.)

share|improve this question
up vote 1 down vote accepted

The above script is so terribly written it brings tears to my eyes. Considering you're running it on every prompt, you may not want to fork so much.

Awk can do all of the above in one line:

ioreg -l | awk '/MaxCapacity/ {max=$5} /CurrentCapacity/ {cur=$5} END {printf "%.1f%%", cur/max*100}'

(sorry, not enough reputation to comment or I would've done that instead)

share|improve this answer
Thanks ! Actually, I found this script online, i don't even understand half of it. – Aliou Aug 19 '11 at 6:35

You have to escape it, if you want it to rerun every time it gets called. It can easiest be done with the dollar-sign.

share|improve this answer
So simple... Thanks ! – Aliou Aug 11 '11 at 23:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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