Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:14:24+00:00 2026-06-07T00:14:24+00:00

I was experimenting with shell functions the other day, with the objective of overriding

  • 0

I was experimenting with shell functions the other day, with the objective of overriding the ls command.
The intention was to have the shell call my ls function first then if the first switch is “-y” do one thing, otherwise call the regular /bin/ls, but the result behaves in a way I don’t understand.

To enable it I decided to use “-y” for the switch because:

$ ls -y
ls: invalid option -- 'y'
Try `ls --help' for more information.

So it can’t break the core functionality of ls.
Anyway, reducing the problem to its simplest, and with a couple of other examples to help highlight the problem:

$ function ls() { [[ "y$1" == "y-y" ]] && echo LS ; } 
$ function less() { [[ "y$1" == "y-y" ]] && echo LESS ; }
$ function try() { [[ "y$1" == "y-y" ]] && echo TRY ; }

So I’m overriding ls, less and defining try which is like the “contol” specimen 🙂

Now Invoking this way:

$ ls ; less ; try 

It behaves as expected (no output), but:

$ ls -y ; less -y ; try -y
LESS
TRY

the ls fails to work, but the less override does, as does the “control”.

Elsewhere on stackoverflow or askubuntu (but I’ve lost the reference for the minute) I saw an example that implied ls was a builltin, but:

$ which ls
/bin/ls

and:

$ builtin ls
bash: builtin: ls: not a shell builtin

So it doesn’t seem to be on my system (and even if it was I can’t actually see why it would behave like this).

I wonder what the explanation is? It isn’t important, but I’d like to understand what’s going on.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:14:25+00:00Added an answer on June 7, 2026 at 12:14 am

    Don’t use which, use type. If you have something like

    $ type ls
    ls is aliased to `ls --color=auto'
    

    Then your function will fail in an interactive shell. Remove this in your .bashrc or wherever its defined. You can also use the unalias builtin.

    You also need to avoid recursion if running a command of the same name as a function within a function.

    ls()
        if [[ $1 == -y ]]; then
            shift
            ...
        else
            command ls "$@"
        fi
    

    Also, make sure you know what you’re doing if you decide to define functions using the function keyword. Use POSIX-style funcname() compound-command when you need POSIX compatibility as of this writing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing with the Command Window in Visual Studio (View->Other Windows->Command Window).
I've been experimenting with cronjobs recently and have it setup like so: crontab: SHELL=/bin/sh
Is there a shell for Javascript coding/experimenting using command line offering history, completion, help,
Experimenting with Cocos2D collection detection and have some questions. First, some background: This is
I am experimenting with Dart. I have created an on click event like so:
While experimenting with iPhone app development, we have several AppIDs which should be deleted
Update I found the cause of the problem. I have been experimenting with the
what im experimenting is the next: S:\proj>manage.py shell Python 2.5.4 (r254:67916, Dec 23 2008,
been experimenting with unordered list UL and i have seem varios examples of using
I have been experimenting with functional programming and I still dont understand the concept.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.