Creating aliases in the Cygwin Bash terminal

Creating aliases in the Cygwin Bash terminal

If you work in a Cygwin Bash or other Linux terminal regularly, it's handy to have short hand for certain commands you execute a lot. E.g. you can navigate to the path "C:/cygwin64/lib/perl5/vendor_perl/5.26/x86_64-cygwin-threads/Text"  by typing cd followed by that entire path. But what if you need to do that 100 times a day, or just keep forgetting the full path? Wouldn't a shorthand for that be great? 

Luckily, Bash aliases allow you to do just that. Simply enter "alias name=value" in the terminal, where name is the shorthand you want to use, and value is whatever command or series of commands you want to execute with the shorthand, with double quotes if the command includes spaces. So in the case above, you would enter:

alias Text="cd C:/cygwin64/lib/perl5/vendor_perl/5.26/x86_64-cygwin-threads/Text"

Then you can go right to that directory by simply entering the command Text. You can also see a list of available commands by entering "alias" with no arguments. 

Starting /bin/bash.exe
 
SimonsPC /cygdrive/c/dev
$ alias
 
SimonsPC /cygdrive/c/dev
$ alias Text="cd C:/cygwin64/lib/perl5/vendor_perl/5.26/x86_64-cygwin-threads/Text"
 
SimonsPC /cygdrive/c/dev
$ alias
alias Text='cd C:/cygwin64/lib/perl5/vendor_perl/5.26/x86_64-cygwin-threads/Text'
 
SimonsPC /cygdrive/c/dev
$ Text
 
SimonsPC /usr/lib/perl5/vendor_perl/5.26/x86_64-cygwin-threads/Text
$

 

Great right? Well, almost. Close that Bash prompt and reopen it, and you'll find all your nice aliases have disappeared! To make them persistent, you need to add them to the end of the .bashrc file in your home folder. By default, that's "C:\cygwin64\home\<user>\". If you can't find the file there and you want to double check where your home directory is, do the following:

SimonsPC /cygdrive/c
$ cd ~
 
SimonsPC ~
$ cygpath -w "$PWD"
C:\cygwin64\home\Simon

Bonus: To execute multiple commands with one alias, simply separate them with semi-colons. E.g. to have a single alias first move you to your Home directory then show its Windows path like the above sequence, add this:

alias wherehome='cd ~;cygpath -w "$PWD";'

Executing that, we're moved to the Home directory, and then the Home directory's full Windows path is dumped:

SimonsPC /cygdrive/c/dev
$ wherehome
C:\cygwin64\home\Simon
 
SimonsPC ~
$

Add new comment

Guest

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.