19

I've been looking around for ways to alias clear and ls into one command. Currently I've defined command x:

alias x="clear;ls"

Now is there any walkaround to avoid recursion and define:

 alias ls='clear;ls'
4
  • I can't get it to do anything recursive on my system, but have you tried alias ls='clear;/bin/ls'?
    – Manny D
    Commented Oct 10, 2011 at 16:42
  • 2
    My ls has long been an alias referring to 'ls' and, like Manny D, it's never had recursion problems. I tried your alias ls='clear;ls' and it worked fine also. This is on RHEL 5 Linux, with Bash version 3.2.25 -- what kind of system and what shell are you using?
    – Stephen P
    Commented Oct 10, 2011 at 16:55
  • oh I was using tcsh, if I define: alias ls 'clear;ls' and use ls it will throw an "Alias Loop." error. But it worked under Bash.
    – GoTTimw
    Commented Oct 10, 2011 at 17:06
  • Perhaps you should change the bash flag to tcsh then.
    – Garrett
    Commented Oct 6, 2014 at 4:19

5 Answers 5

38

If you put a backslash before the command name, that will disable any aliases.

alias ls='clear;\ls'

Or, like Arnaud said, just use the full path for ls.

2
  • I think that this is the cleanest solution and it works just fine. Thanks
    – GoTTimw
    Commented Oct 10, 2011 at 17:06
  • Likewise, quoting will disable the alias. This is true within an aliasdefinition and also as a direct command. Commented Aug 24, 2021 at 17:58
20

Another way of doing this would be

alias ls='clear; command ls'

This is different from /usr/bin/ls, as it still searches ls in the $PATH, but will ignore shell functions or aliases.

1
  • 1
    This is applicable to many situations, not just aliases. If you want to go straight to built-ins and executables, bypassing functions and aliases, use the command built-in.
    – Zenexer
    Commented Aug 4, 2013 at 6:51
3

There is no direct recursion in alias. From man bash:

The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to ls -F, for instance, and bash does not try to recursively expand the replacement text.

Nonetheless, you can also note:

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.

$ pwd
/Users/myhomedir

$ alias ls="date;pwd"
$ ls
Fri Jun 23 16:34:00 PDT 2023
/Users/myhomedir


$ alias pwd='whoami'
$ ls
Fri Jun 23 16:35:00 PDT 2023
enzyme

$ alias ls="date;'pwd'"
$ ls
Fri Jun 23 16:35:38 PDT 2023
/Users/myhomedir
1

Just do :

alias ls='clear;/usr/bin/ls'

When typing:

$ ls

First of all it will search an user defined function, it will launch it, else search in $PATH commands.

By giving the explicit path of the ls command, recursion will be avoided.

1
  • 3
    It is funny because ls is never in /usr/bin.
    – Hello71
    Commented Jul 27, 2012 at 2:47
0

I always use ls with --color=auto parameter ( -G Enable colorized output.) and like to use functions.

clear_and_ls() {
    clear
    command ls --color=auto
}

alias ls="clear_and_ls"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.