0

Bash terminal setup (use, aliases and functions)

The bash terminal in linux (personally I’m using Ubuntu) is something that I’m falling in love with more and more over time, initially it was odd to have to type commands into a black box on the side of the screen to get things done but seeing what can be done with it the terminal lets you perform all kinds of operations quickly and easily without having to click through a bunch of windows. Here are a few tips to get you acquainted:

 

Copy/paste

In the bash terminal Ctrl+C is a quit command so trying to copy text from the terminal results in sending quit instead:

  • Copy:
    Ctrl+Ins
  • Paste:
    Shift+Ins

Command recall:

At first I had notes and scribblings for different commands jotted down and finding a command I’d previously used was a case of either scrolling and copy/pasting or going through my notes. These three help get around that:

  • Up arrow:
    When using the terminal you can press up and down to cycle through recently used commands which saves so much time in retyping!.
  • Ctrl + R:
    if the command you want is out of reach pressing Ctrl+R lets you search previous commands by typing the beginning of them and having bash give autocomplete suggestions.
  • Bash History:
    you can show your command history in gedit by typing the following:

    $ gedit ~/.bash_history

Bashrc

The bashrc file is where you’ll go to customise your experience with the terminal (hopefully I’ll go into more detail another time) and can be opened in gedit with the following:

$ gedit ~/.bashrc

this allows you to make changes very easily, one such change you might like to make would be to add:

Aliases

Aliases are a great way to shorthand your commonly used commands and are simple to use. To add aliases to open the files for both bashrc and the bash history simply:

  • open the bashrc file:
    $ gedit ~/.bashrc
  • add the following text at the bottom:
    # my aliases
    alias bashrc="gedit ~/.bashrc"
    alias comhist="gedit ~/.bash_history"
  • close your current terminal and open a new one to test out your new aliases
    $ comhist

Aliases and parameters – functions

Aliases themselves don’t take parameters, for that we’d need to declare a function. Functions work in the same way that aliases do but they’re useful for more complex operations and can take in parameters. Here’s an example of a very simple function declaration that uses a single parameter, as above this can be stored in your bashrc file:

echo_me(){
 echo "you said: "$1
 echo "do you like "$1"?"
}

As above you’ll need to save the file and restart your terminal to be able to use your new function but this can be called with:

$ echo_me pizza

to return

you said: pizza
do you like pizza?

Obviously this example doesn’t serve any particular use to anyone but shows how you could pass an argument into a block of commands to be passed as a parameter for those, a filepath, server, file extension or whatever you need.

JBaker

Joi is an IT Development Analyst working in London and a consummate technophile. Whilst he isn't coding he can be found reacquainting himself with his guitars, spinning poi, looking for climbing walls or chasing the latest shiny thing to cross his field of vision.

Leave a Reply

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