Create Bash Aliases With Arguments In Ubuntu
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:
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.
or multi-line:
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:
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
Comments
Post a Comment