Home » Questions » Computers [ Ask a new question ]

How to use spaces in a bash alias name?

How to use spaces in a bash alias name?

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

Asked by: Guest | Views: 319
Total answers/comments: 2
Guest [Entry]

"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"
Guest [Entry]

"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"