Bash: Spaces in alias name

126

38

I am trying to create an aliases in bash. What I want to do is map ls -la to ls -la | more

In my .bashrc file this is what I attempted:

alias 'ls -la'='ls -la | more'

However it does not work because (I assume) it has spaces in the alias name. Is there a work around for this?

sixtyfootersdude

Posted 2010-02-05T22:35:30.723

Reputation: 6 399

9Why not do alias lsm='ls -la | more' – Nifle – 2010-02-05T22:52:05.863

Answers

137

The Bash documentation states "For almost every purpose, shell functions are preferred over aliases." Here is a shell function that replaces ls and causes output to be piped to more if the argument consists of (only) -la.

ls() {
    if [[ $@ == "-la" ]]; then
        command ls -la | more
    else
        command ls "$@"
    fi
}

As a one-liner:

ls() { if [[ $@ == "-la" ]]; then command ls -la | more; else command ls "$@"; fi; }

Automatically pipe output:

ls -la

Paused until further notice.

Posted 2010-02-05T22:35:30.723

Reputation: 86 075

1why do you need to use double brackets inside the if statement? – sixtyfootersdude – 2010-02-08T14:55:35.580

1

@sixtyfootersdude: The double-bracket form is more powerful and I use it by habit. See http://mywiki.wooledge.org/BashFAQ/031

– Paused until further notice. – 2010-02-08T18:35:18.623

So what is the final command? alias ls='ls()' ?? – Jeef – 2015-04-01T12:39:23.577

1@Jeef: No, my answer uses a function instead of an alias. I have edited it to try to make it clearer. – Paused until further notice. – 2015-04-01T14:14:57.150

What is command for here? Why not just ls -la | more or ls "$@"? – merlinpatt – 2016-03-13T16:54:57.613

7@merlinpatt: command prevents the function from being called recursively. – Paused until further notice. – 2016-03-13T18:05:30.157

Very useful, I wanted to use a an alias to replace git push with a behaviour that would change when git push was called from different directories. If you put a function in .bash_aliases it will be usable from your user folder and subfolder, just as an alias would. – Simonlbc – 2016-05-28T12:35:54.557

@Simonlbc: Note that .bash_aliases is most likely a distribution or locally defined feature rather than a default Bash feature. – Paused until further notice. – 2016-05-28T15:14:18.070

@Simonlbc Instead of modifying the current git push behavior, I would suggest creating a new Git Alias, e.g. git my-push.

– Franklin Yu – 2016-08-08T21:55:06.633

@FranklinYu why? – Simonlbc – 2016-08-09T07:23:43.637

@Simonlbc To make sure that tutorials online will surely work on my environment. Maybe more of a personal choice. – Franklin Yu – 2016-08-09T19:19:00.293

Take care when naming the function that you don't conflict with the bash-completion (command autocomplete) function. I spent a bit of time troubleshooting tmux, having created a function _tmux to which to alias the command, but discovered that the bash-completion for tmux also used _tmux to drive the autocomplete. So, I created the function _TMUX instead. – palswim – 2018-09-17T20:15:28.673

54

From the alias man page:

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including shell metacharacters, with the exception that the alias name may not contain `='.

So, only the first word is checked for alias matches which makes multi-word aliases impossible. You may be able to write a shell script which checks the arguments and calls your command if they match and otherwise just calls the normal ls (See @Dennis Williamson's answer)

heavyd

Posted 2010-02-05T22:35:30.723

Reputation: 54 755

8+1 For explaining why I am not allowed to use ls -la as an alias. – sixtyfootersdude – 2010-02-08T14:49:19.883

This not only answered my question, but gave me valuable insight into how the aliasing mechanism actually works. Your quote from the man page was quite helpful. – Lily Finley – 2019-01-21T14:32:06.630

7This was helpful because instead of trying to solve it it answered the question. I came here because i wanted to create an alias with a space in it and that just won't happen. – angryundead – 2014-02-11T21:15:34.887

14

A slightly improved approach taken from Dennis' answer:

function ls() {
  case $* in
    -la* ) shift 1; command ls -la "$@" | more ;;
    * ) command ls "$@" ;;
  esac
}

Or the one-liner:

function ls() { case $* in -la* ) shift 1; command ls -la "$@" | more ;; * ) command ls "$@" ;; esac }

This allows for further options/arguments to be appended after the command if needed, for example ls -la -h

ld_pvl

Posted 2010-02-05T22:35:30.723

Reputation: 251

2How does this handle if I want ls -lat to be excluded from this treatment? I would need to put a case to handle it above the -la* entry, yes? – Steven Lu – 2014-04-28T05:36:44.783

-1

You can invoke this alias still, but you need quotation in order that the space is part of the command word. So "ls -la" -p pattern will pass the -p pattern option to more, not ls.

Charles Stewart

Posted 2010-02-05T22:35:30.723

Reputation: 2 624