154

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.

2
  • 4
    csh and tcsh support parameters like $1 in alias; bash doesn't. Commented Dec 31, 2014 at 2:13
  • For visitors considering the answers below, the Bash manual specifically says "If arguments are needed, use a shell function". Commented Dec 6, 2023 at 10:04

6 Answers 6

229

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It's usually not necessary to export a function.
@Dennis, yeah, you are right, for a simple alias that won't be necessary, but it is there just in case you want to use it in some derived shell/process.
Why do I need an alias if I can call the function directly?
This is not an alias-base solution, its a function-base solution .. It's working smoothly
Technically almost everything is a file to Linux, so functions are files, alias & tail are files, even processes are files.
46
alias tail_ls='_tail_ls() { ls -l "$1" | tail ;}; _tail_ls'

1 Comment

That's a creative approach. Definitely solves the issue if they need to use an alias rather than a function… I think I'd prefer a function if those are an option instead, but a very neat solution to keep in mind for when the right situation arises!
38

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

  1. In ~/.bashrc add:

    sendpic () { scp "$@" [email protected]:/www/misc/Pictures/; }
    
  2. Save the file and reload

    $ source ~/.bashrc
    
  3. And execute:

    $ sendpic filename.jpg
    

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

2 Comments

@jruzafa, In which context, you assert that 'The solution of maxim-sloyko works did not work' whilst his/her solution worked for myself.
You can add your functions to bashrc with: typeset -f >> ~/.bashrc
4

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

1 Comment

Please fix the syntax error (missing ; before }) and double-quote $1 so as to also support filenames with embedded whitespace and other shell metacharacters.
4

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

$ alias tail_ls='ls -l "$1" | tail'
$ set mydir
$ tail_ls

2 Comments

What is set doing here? How does this work?
@Brijesh, set mydir defines $1 as mydir that would be the intended directory... not that great.
0

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

You'll want to add something like this to your ~/.config/fish/config.fish which is the equivalent of your ~/.bashrc

function tail_ls
  ls -l $1 | tail
end

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.