I've just created a teeny little script to count the seconds until I get auto-logged off. I already solved my issue with my ssh client settings, but I'd still like any help making the bash script nicer to read, or just tips in general.


while ( [ : ] )
  for i in `seq 0 $n`; do echo -en '\b'; done
  echo -n $count
  sleep 1

2 Answers 2

while true; do
  printf "\r%d" $((++count))
  sleep 1

Your while condition is doing a lot of work just to return true: you launch a subshell, and evaluate a test giving a 1-character string (which will always return true). Make it simpler and more readable by just executing the true program.

Note that variable names inside an arithmetic expression do not require the leading dollar sign in most cases. That's quietly documented here:

"Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax."

printf is more portable than echo, if that's a concern for you. I also find it makes your intentions more obvious.

Instead of backing up the right number of characters, just carriage return to the first column and overwrite the previous contents.

  • \$\begingroup\$ No freaking way. I did previously know about /bin/true, I just didn't think of it. However, I never knew bash had a ++ syntax, nor did I realize I can use variable names without the dollar sign. THANKS! \$\endgroup\$ Jan 11, 2013 at 18:12

If the answer is what you care for,

in your .bash_profile :

touch "~/.loggued_in"

in your .bash_logout:

date -r ~/.loggued_in ; date

and check if there is a timeout setup:

{ set ; env ; } | egrep 'TM|TIME|OUT' | sort | uniq

and lastly, if connecting via something like Putty (or other), they can be setup to have a "keepalive" sent regularly (usually a "nop"). In Putty, "Settings - Connection - seconds between keepalives" (0 by default : set it to, say, 50 if you get auto loggued off after 1mn)

ok, about the code itself : very nice for displaying the time in an interresting manner (sort of like a progress bar). If you prefer it in one place (and no need for an extra $n) :

while ( [ : ] ); do count=$(($count+1)); echo -en '\b\b\b\b'; printf "%4s" $count; sleep 1; done
  • \$\begingroup\$ sorry for the 3 first parts: i got used to questions on so ... I believe my answer is not at all what you need here, ie code review - wise... But your script is quite fine already. Let's see what others propose as interresting modifications \$\endgroup\$ Jan 11, 2013 at 14:25
  • \$\begingroup\$ Thanks guys for your comments. I tried to give you guys a point but it's saying I have to login (which I don't want to do right now). It let me do the check box though. Oh and yes I had already determined how to prevent ssh disconnects. It did take me a long time, I should have asked on here. :-) \$\endgroup\$ Jan 11, 2013 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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