I’m trying to create a ls-like function.

I started with this alias, which works fine:

alias l="/usr/bin/ls -lF --color=always | tr -s ' ' | cut -d ' ' -f 9-"

However, converting it to a function results in no colors:

l() {
    local _c=
    [ -t 1 ] && _c=--color=always
    /usr/bin/ls -lF $_c "$@" | tr -s ' ' | cut -d ' ' -f 9-

Even removing all differences from the alias to the function remains colorless:

l() {
    /usr/bin/ls -lF --color=always | tr -s ' ' | cut -d ' ' -f 9-

The only colored variant is one without oipe

l() {
    /usr/bin/ls -lF --color=always

What prevents the color from passing through the pipes in functions?

  • All functions, and the alias, work fine for me. You can check with which l if the command l is indeed the desired function. Aside from that, it looks you are trying to extract just the file names from the long output of ls to create an output where each file is listed on its own line. If so, the option -1 (the number "one") might be of interest to you: ls -1F --color=always. – Adaephon Jan 11 '17 at 10:08

Are you sure you haven't got an l alias that is either interfering with the function definition or being used before the function: aliases take precedence over functions. And are expanded even for a function definition.

  • that was it, thanks. I didn’t know this tidbid, and prezto has an alias that is hilariously similar to what I thought was creative for “l” ls -1A – flying sheep Jan 11 '17 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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