Thursday, 9 June 2016

linux - Alias with variable in bash




I want to create an alias in bash like this:



alias tail_ls="ls -l $1 | tail"


Thus, if somebody types:



tail_ls /etc/ 


it will only show the last 10 files in the directory.



But $1 does not seem to work for me. Is there any way I can introduce variables in bash.


Answer



I'd create a function for that, rather than alias, and then exported it, like this:



function tail_ls { ls -l "$1" | tail; }

export -f tail_ls


Note -f switch to export: it tells it that you are exporting a function. Put this in your .bashrc and you are good to go.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...