Posts

Showing posts with the label aliases

Create Bash Aliases With Arguments In Ubuntu

Image
The scenario here may not be the same with you but the point is you want your bash alias to work with parameters. Say I want to display a file's content in colors and I want the output to display line numbers as well. For the color part, I used python's syntax highlighter called pygments and I used bash built-in line numbering command called nl . Now to achieve my goal, I have to pipe nl to the result of pygmentize: pygmentize test.php | nl This is quite a hassle especially if you have to do it frequently only with different files. Unfortunately, bash alias does not directly accept parameters. The solution is to create a bash function. ccat() { pygmentize $1 | nl } or multi-line: function ccat() { pygmentize $1 | nl } I named the function ccat (custom cat) because it works like a cat command but the syntax is highlighted. Put this function in ~/.bashrc and then restart your terminal or use source: ccat test.php

Creating Bash Aliases

In bash scripting, you can create aliases for the shell commands you frequently used. The most simple way to create an alias is to assign it directly in your terminal: alias ll='ls -l' The ls command list the files and directories in the current directory. Now if you run the ll command in your terminal, this will list the files in long format as specified by the -l option. The disadvantage is that the alias will only work in the current shell. This means that if you open a new tab or window, the ll command will not work there. Also, the ll command will not work anymore once you closed the terminal. If you want to create an alias that will work in any shell instance, you can add your aliases in ~/.bash_aliases. After adding your alias fire up this command to make it effective immediately: $ source ~/.bash_aliases