Monday 1 July 2019

unix - Bash: Spaces in alias name


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?



Answer



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

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...