Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use argument in bash command in first part of a pipe

I want to have a simple function or alias in my bashrc that lists files with ls using all my favorite options and pipes the output to more.
Here is how it looks like

l() {
    ls -lh --color $1 | more 
} 

However, when I call this function in a terminal with a wildcard argument, like l *.txt, the blob is resolved before it gets passed to my function. The result is, that I only get the first txt file displayed.

I am afraid that using a function is an overkill for what I want. Is there a way to do this without a function, with just a simple alias?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

*.txt is not the argument; it’s a pattern that could expand (depending on your shell settings) to 0 or more separate arguments. Your function only looks at the first of them.

Use "$@" instead of "$1 to pass all arguments to ls.

l () {
    ls -lh --color "$@" | more
}

The alias would suffice if you weren’t trying to pipe the output to more, as aliases don’t take arguments.

alias l='ls -lh --color'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading