I wanted to start the ssh agent the moment I login to my Ubuntu machine. So I followed http://mah.everybody.org/docs/ssh and appended the following piece of code in my ~/.profile.
#
# setup ssh-agent
#
# set environment variables if user's agent already exists
[ -z "$SSH_AUTH_SOCK" ] && SSH_AUTH_SOCK=$(ls -l /tmp/ssh-*/agent.* 2> /dev/null | grep $(whoami) | awk '{print $9}')
[ -z "$SSH_AGENT_PID" -a -z `echo $SSH_AUTH_SOCK | cut -d. -f2` ] && SSH_AGENT_PID=$((`echo $SSH_AUTH_SOCK | cut -d. -f2` + 1))
[ -n "$SSH_AUTH_SOCK" ] && export SSH_AUTH_SOCK
[ -n "$SSH_AGENT_PID" ] && export SSH_AGENT_PID
# start agent if necessary
if [ -z $SSH_AGENT_PID ] && [ -z $SSH_TTY ]; then # if no agent & not in ssh
eval `ssh-agent -s` > /dev/null
fi
# setup addition of keys when needed
if [ -z "$SSH_TTY" ] ; then # if not using ssh
ssh-add -l > /dev/null # check for keys
if [ $? -ne 0 ] ; then
alias ssh='ssh-add -l > /dev/null || ssh-add && unalias ssh ; ssh'
if [ -f "/usr/lib/ssh/x11-ssh-askpass" ] ; then
SSH_ASKPASS="/usr/lib/ssh/x11-ssh-askpass" ; export SSH_ASKPASS
fi
fi
fi
Now when I login to unity session it gives me a nasty error pop-up indicating line 30 in ~/.profile which is:
[ -z "$SSH_AGENT_PID" -a -z `echo $SSH_AUTH_SOCK | cut -d. -f2` ] && SSH_AGENT_PID=$((`echo $SSH_AUTH_SOCK | cut -d. -f2` + 1))
according to my text editor. I tried to search for a solution and amusingly this Start ssh-agent on login SO answer points to the same website but another solution which requires .bash_profile. Problem is that my ~/.profile will be ignored if a ~/.bash_profile or ~/.bash_login exists which I don't want as the .profile contains some more important initialization. I don't understand what's wrong with this expression?
The problem is the
-a
parameter in the first[]
.Rewrite it like this and it will work (still doing the same thing):