Cmder, SSH-Agent, & Other .bash_profile Benefits

October 06, 2015 0 Comments cmder, bash, tips

Not long ago, an IT Admin from Missouri and a Software Engineer from the UK emailed me asking me if I could help them figure out how to get SSH-Agent running in Cmder. Now that I have received two big thumbs up from each of them, it's time for me to share my solution to you.

Meet your new friend: .bash_profile

On Linux systems using the Bourne-Again Shell (BASh), there are various files which can be used to enhance your experience with bash. One such file is .bash_profile which can be found (and if it isn't found, it can be placed in) your ~ (home) directory. Other files have other purposes, such as ~/.bashrc, however due to how Cmder works, we're only going to use ~/.bash_profile, and you can otherwise read about the differences between the two files on Josh Staiger's blog. For other very useful information on bash, check out the StackOverflow Wiki for Bash.

Now, chances are that you do not already have ~/.bash_profile created on your system; To create it, go ahead and open up your git bash profile in Cmder and type the following:

$ touch ~/.bash_profile

The file now exists. To edit it, let's open it in notepad:

$ notepad ~/.bash_profile

Finally, at the top of the file, let's type the following, which informs bash in Cmder that it'll be executing a bash script:

Running Eval on SSH-Agent & Adding Keys

One of the benefits of ~/.bash_profile is automating processes you would otherwise have to manually participate in, such as running an eval on ssh-agent, and adding your ssh keys in ~/.ssh. To do this very thing automatically every time you open Cmder, add the following on a new line in ~/.bash_profile:

# Run Eval on SSH-Agent and Add Key(s)
if [ -z "$SSH_AUTH_SOCK" ] ; then  
  eval `ssh-agent -s`
  echo "Adding Keys..."
Aliases Shortcut Your World

Aliases are the greatest thing since sliced bread. Are you often finding yourself typing git status -s and wished there was a quicker way? Well there is, you could literally alias that string to s and use that instead.

Here are a few of my aliases, which I'll then explain below. Should you like to add these to your ~/.bash_profile, copy and paste them to a new line (and keep the #comment intact).

# Aliases
alias subl="/c/Program\ Files/Sublime\ Text\ 3/sublime_text.exe"  
alias 7z="/c/Program\ Files/7-Zip/7z.exe"  
alias ll="ls -lhA"  
alias edprof="subl ~/.bash_profile && source ~/.bash_profile"  
alias c="clear"  
alias ..="cd .."  
alias ...="cd ../.."  
alias ....="cd ../../.."  
alias grep="grep --color"  

What these aliases do:

  • subl opens a given file with Sublime Text 3;
    • Example: subl ~/.bash_profile which opens ~/.bash_profile in Sublime Text 3.
  • 7z uses 7zip (64-bit) to extract files;
    • Example: 7z e which extracts (e) to the current directory.
  • ll runs ls (list directory contents) with the following options: -l (use a long listing format), -h (human-readable sizes, [e.g, 1K, 234M, 2G]), and -A (do not list implied . and ..);
  • edprof opens ~/.bash_profile in Sublime Text 3 and once that is saved and closed, sources ~/.bash_profile, running the contents in the current shell (making it so you don't need to restart Cmder or the current bash profile to see changes you made go live);
  • c clears the screen;
  • .., ..., and .... navigate you up the folder hierarchy; and
  • grep is modified to show colors when you run grep.

There is a plethora of aliases, functions, and other things you can add to your ~/.bash_profile to make your stay in your bash tab in Cmder far more pleasant and convenient. Googling "bash_profile aliases" will yield many more aliases written by other developers, while "bash_profile ssh" will take you to other simpler and more complex options for adding and managing your ssh-keys. Linux has a rich and well-developed history, and a million other people have probably asked questions for Linux that now apply to Windows (while using something like Cmder), so keep that in mind.

If you have any comments or questions about this article, or are interested in me writing an article about another topic or issue you are curious about or needing help with, feel free to shoot me an email:

Andrew Moore
Denver, Colorado
Not your typical IT guy – a unicorn if you will – with a love for the outdoors, technology, security, and keeping end-users happy (and in line). INTP.