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 8750723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:55:01+00:00 2026-06-13T12:55:01+00:00

I am trying to define a bash function, mycd . This function uses an

  • 0

I am trying to define a bash function, mycd. This function uses an associative array mycdar. If the key exists in the array the function will change directory to the corresponding value of the key. If the key doesn’t exist it will change to the dir provided in the command line.

What I would like to do is to have completion for this function, from both they keys of the associated array or from the folders existing in the current directory.

Thank you.

  • 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-13T12:55:02+00:00Added an answer on June 13, 2026 at 12:55 pm

    Building my own cd function with completion

    Using an associative array for storing some paths.

    First the command:

    mycd() { [ -v mycdar["$1"] ] && cd "${mycdar[$1]}" || cd "$1"; }
    

    Second the completion command

    _mycd() {
        local cur;
        _cd ;
        _get_comp_words_by_ref cur;
        COMPREPLY=($(
            printf "%s\n" "${!mycdar[@]}" |
                grep ^$cur)
            ${COMPREPLY[@]});
        }
    

    One array:

    declare -A mycdar='(
         ["docs"]="/usr/share/doc"
         ["home"]="$HOME"
         ["logs"]="/var/log"
         ["proc"]="/proc"
         ["root"]="/"
         ["tmp"]="/tmp"
    )'
    

    Than finaly the bind:

    complete -F _mycd -o nospace mycd
    

    Or to permit standard path building behaviour:

    complete -F _mycd -o nospace -o plusdirs mycd
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to define a function in sybase. I write this script: CREATE FUNCTION
When trying to define an array such this: $array = new SPLFixedArray(256); for ($i
I'm trying to define a function that will allow me to get a value
Im trying to define a class friend function outside the namespace like this: namespace
I'm trying to define a method in my vb.net interface that will only accept
I'm trying to define a very simple function in Drools as below: import java.util.List;
I'm trying to define a new array through a where query but I can
when trying to define a function that would remove the largest subset of set
Im trying to define an array of arrays as a Constant in one of
I'm trying to define bash auto-completion for teamocil so that when I type teamocil

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.