Variable alias in bash

I want to create an alias in bash as follows:

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

Thus, if someone is like:

 tail_ls /etc/ 

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

But $1 doesn't seem to work for me. Is there any way to introduce variables into bash.

+75
linux bash
Dec 14 '10 at 10:29
source share
6 answers

I would create a function for this, not an alias, and then export it, for example:

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

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

+125
Dec 14 '10 at 10:36
source share

The @ maxim-sloyko solution does not work, but if the following:

  • In ~ / .bashrc add:

     sendpic () { scp "$@" mina@foo.bar.ca:/www/misc/Pictures/; } 
  • Save the file and reload

     $ source ~/.bashrc 
  • And do:

     $ sendpic filename.jpg 

source: http://www.linuxhowtos.org/Tips%20and%20Tricks/command_aliases.htm

+28
Jan 09 '13 at 12:20
source share
 alias tail_ls='_tail_ls() { ls -l "$1" | tail ;}; _tail_ls' 
+5
Feb 21 '16 at 21:47
source share

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

+1
Dec 14 '10 at
source share

If you use the Fish shell (from http://fishshell.com ) instead of bash, they write functions a little differently.

You need to add something like this to your ~/.config/fish/config.fish , which is equivalent to your ~/.bashrc

 function tail_ls ls -l $1 | tail end 
+1
04 Oct '13 at 13:11
source share

You can define $1 with set , and then use your alias as intended:

 $ alias tail_ls='ls -l "$1" | tail' $ set mydir $ tail_ls 
+1
Jun 16 '16 at 8:58
source share



All Articles