Alias with variable in bash [duplicate]
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.
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.
The solution of @maxim-sloyko did not work, but if the following:
-
In ~/.bashrc add:
sendpic () { scp "$@" [email protected]:/www/misc/Pictures/; }
-
Save the file and reload
$ source ~/.bashrc
-
And execute:
$ sendpic filename.jpg
original source: http://www.linuxhowtos.org/Tips%20and%20Tricks/command_aliases.htm
alias tail_ls='_tail_ls() { ls -l "$1" | tail ;}; _tail_ls'
You can define $1
with set
, then use your alias as intended:
$ alias tail_ls='ls -l "$1" | tail'
$ set mydir
$ tail_ls
tail_ls() { ls -l "$1" | tail; }