Bash Alias Commands

Something useful.  Particularly on my Mac.   Occasionally, but less commonly these days, I miss the old commands I grew up with.  When that happens, there’s two choices.   First, suck it up and learn the bash equivalent, or Second, set an alias and forget it.    Now, the other value is that you can make an alias do a lot of “stuff” all at once with a simple command.

While I’m prepping to rebuild my machine, lets review what I have set these days in my alias file and how to set these things up.

Step 1.   If you haven’t created a custom “.bash_profile” file, yet (This is where the alias commands are kept) run this command in the terminal window to create the file.

touch ~/.bash_profile

Step 2.  (or Step 1 if you just want to edit the file.)   Open the file.  This will open the file in the default text editor app on your system.

open ~/.bash_profile

Step 3.   Add some alias commands.   This is what I currently have in mine.


# ii: display useful host related information
# -------------------------------------------------------------------
ii() {
echo -e "\nYou are logged on ${RED}$HOST"
echo -e "\nAdditional information:$NC " ; uname -a
echo -e "\n${RED}Users logged on:$NC " ; w -h
echo -e "\n${RED}Current date :$NC " ; date
echo -e "\n${RED}Machine stats :$NC " ; uptime
echo -e "\n${RED}Current network location :$NC " ; scselect
echo -e "\n${RED}Public facing IP Address :$NC " ; myip
#echo -e "\n${RED}DNS Configuration:$NC " ; scutil --dns

# Alias
alias myip='curl' # myip: Public facing IP Address
alias netCons='lsof -i' # netCons: Show all open TCP/IP sockets
alias flushDNS='dscacheutil -flushcache' # flushDNS: Flush out the DNS Cache
alias lsock='sudo /usr/sbin/lsof -i -P' # lsock: Display open sockets
alias lsockU='sudo /usr/sbin/lsof -nP | grep UDP' # lsockU: Display only open UDP sockets
alias lsockT='sudo /usr/sbin/lsof -nP | grep TCP' # lsockT: Display only open TCP sockets
alias ipInfo0='ipconfig getpacket en0' # ipInfo0: Get info on connections for en0
alias ipInfo1='ipconfig getpacket en1' # ipInfo1: Get info on connections for en1
alias openPorts='sudo lsof -i | grep LISTEN' # openPorts: All listening connections
alias showBlocked='sudo ipfw list' # showBlocked: All ipfw rules inc/ blocked IPs
alias console='~/Documents/' # console usbserial 9600 ^a ^\ to quit

Step 4.  Save the file.   Feel free to use your mouse to use the file menu or Command S, Command Q, to save and exit.

Step 5.  Back in the terminal window, lets apply these changes now so we don’t have to reboot.

source ~/.bash_profile

Step 6.   Try an alias command.    Give “ii” a try, or “myip” in the terminal window and press enter.

That’s it 🙂

Now, you may have noticed that console command.   It calls a (shell script).   In my line of work, I use a usb to serial adapter occasionally to connect directly to network equipment for configuration.  Let’s take a look at that file, just to see how it works. contains two lines of code:

screen /dev/tty.usbserial 9600

Pretty simple.  I will say, there’s lots of things you can do with the profile file.  From changing the command prompt, to colors.

Here’s an example of changing the command prompt.

Step 1.   See what the current one is.

echo "$PS1"

You’ll see something like \h:\W \u\$.    Where u is username, h is hostname, W is working directory, and $ is the # if root, or display $ at the end.  A capital H would give you FQDN hostname, and a @ will give you a time stamp.

Now, if you want to make it color, toss in the color command.   “\e[” starts a color, the 0;32 is the color code, the “\e[m” stops using the color.

To tie all that together, say I want a different command prompt in green, toss this line into my bash_profile file, or for trial basis, run it  :

export PS1="\e[0;32m[\u@\h \W]\$ \e[m "

Getting complex with multiple colors….

export PS1="\[\e[0;33m\][\[\e[0;32m\]\u\[\e[0;33m\]@\h:\[\e[0;39m\]\w\[\e[0;33m\]]\$\[\e[0m\] "

Yup…   Ok… that’s killing my brain too, and that’s where we should end this topic 😉


Leave a Reply

Your email address will not be published. Required fields are marked *