LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash auto-complete of environment variables (https://www.linuxquestions.org/questions/linux-general-1/bash-auto-complete-of-environment-variables-613203/)

armandino101 01-12-2008 09:52 PM

Bash auto-complete of environment variables
 
Recently I reinstalled ubuntu and I noticed a small but annoying problem with auto complete.

Say I have a $SOME_DIR variable. If I type $SOME and hit tab I get

Code:

cd \$SOME_DIR
it appends a slash at the beginning, which i don't want.

Second, if I use an env variable with any other command, e.g. ls, the auto complete isn't triggered at all..

I haven't come across this problem before and would appreciate any pointers.

Thanks in advance,

armandino

aus9 01-13-2008 12:49 AM

first try looking into each text file at /etc/bash_completion.d

then turn on hidden files and look at /home/yourname/bash"stuff" I have 6 files there

if none of that works and you know the exact phrase, since you only say it could be
$SOME_DIR....whatever it is ....try grep but that may take a long time or find

eg find /etc -type f -print |xargs grep -n "$SOME_DIR"

try a "zzz" first to see it should produce nil result...meaning it aint there.

armandino101 01-13-2008 04:52 PM

Hi,

Thanks for the tips.

I think I may not have described the problem clearly. The problem is that bash escapes the dollar sign of an environment variable when I use the cd command. $SOME_DIR was just an example. It could be any variable.

To explain again, say the environment variable points to some directory

Quote:

export SOME_DIR=/path/to/some/directory
Now if I type 'cd $SOME_D' and hit tab for autocompletion, I get this

Quote:

cd \$SOME_DIR
(note the slash appended by the shell) instead of this

Quote:

cd $SOME_DIR
So my question is, how I can modify shell's autocompletion so it doesn't escape the dollar sign with a slash?

aus9 01-14-2008 12:41 AM

please report your post to a moderator and ask them to move it to a programming forum cos I can not help. Sorry to have mislead you.

armandino101 01-14-2008 10:14 PM

Hey, no worries. I appreciate your help.

Mods, if you feel there's a more appropriate forum for this question, please move it there :)

armandino101 01-15-2008 06:16 PM

I haven't found a proper solution to this, but there's a workaround. The escaping of environment variables can be disabled by pressing Esc followed by tab.

For example,

Code:

# cd $SO + [Esc] + [Tab]
# cd $SOME_DIR

If anyone knows a proper solution (i.e. without having to it Esc), please post it.

archtoad6 02-12-2008 07:35 PM

Solution
 
Quick Answer
Code:

shopt -s cdable_vars
complete -v -F _cd $nospace $filenames cd

This will give an instant, but temporary solution.

Short Answer
Put "shopt -s cdable_vars" in a start-up script. -- This will give a permanent solution. I haven't done this yet, not sure which one to use.

Context
I am still running SimplyMEPIS 3.3.2 -- based on Debian Etch, back when it was "Testing", not "Stable" as it is now. All of this is based on what I found on my own system.

Notes
  • "cd $SO + [Esc] + [Tab]" didn't work for me.
  • I started w/ /etc/bash_completion, not its companion directory. It's a large file:
    Code:

    $ wc /etc/bash_completion
      8039  26553 188168 /etc/bash_completion


Key Clue
(From less -SN /etc/bash_completion)
Code:

  3000 if shopt -q cdable_vars; then
  3001    complete -v -F _cd $nospace $filenames cd
  3002 else
  3003    complete -F _cd $nospace $filenames cd
  3004 fi

References
Code:

$ help complete
Run man bash, then search for:
  • Completing
  • Programmable Completion
  • compgen [
  • complete [
Also from less -SN /etc/bash_completion:
Code:

5 #  Copyright (C) Ian Macdonald <ian@caliban.org>

21 #  The latest version of this software can be obtained here:
22 #
23 #  http://www.caliban.org/bash/index.shtml#completion

Off Topic Discoveries
1. OMG, MEPIS has a "service" (ser<TAB>) cmd., & also "invoke-rc.d" (inv<TAB>). -- Check out their man pages.

2.
Code:

$ umount <TAB><TAB>
/                        /dev/shm                  /proc/sys/fs/binfmt_misc
/200                      /mnt/iso                  /sys
/Common                  /proc
/dev/pts                  /proc/bus/usb

Q: can /dev/pts, /dev/shm, /proc/*, or /sys be usefully umounted? -- I.E., is this a bug?

armandino101 02-13-2008 09:55 PM

Hi!

Thanks for posting the solution. It worked perfectly well.

I think the complete command could do with more documentation. It has no man page or help output other than a two-line usage information.

Re:
Quote:

can /dev/pts, /dev/shm, /proc/*, or /sys be usefully umounted? -- I.E., is this a bug?
I've never tried or had to unmount any of them, but I did a quick search and found an interesting blog post that describes a situation where you would do that.

Also, from the man page:

Code:

      -a    All  of  the  file systems described in /etc/mtab are unmounted.
              (With umount version 2.7 and later: the proc filesystem  is  not
              unmounted.
)

Arman

archtoad6 02-14-2008 12:23 PM

AFAIK, only external commands (config files etc.) have man pages. Because it is a bash built-in, complete is documented inside the (incredibly long) bash man page. That is why I gave you the following search phrases:
Quote:

Originally Posted by archtoad6 (Post 3055141)
Run man bash, then search for:
  • Completing
  • Programmable Completion
  • compgen [
  • complete [

Each will take to an appropriate place in the bash man page. Use the whole phrase, I designed it to get you to the exact place I intended.


Code:

help complete
is only supposed to present a reminder on the command line when you need to jog your memory.

revprez 01-25-2012 10:29 AM

Solution
 
To the original poster's issue, his problem is likely because dash rather than bash is configured as the default shell.

Just run 'sudo dpkg-reconfigure dash' and when prompted do not set dash as default.

nacorn 08-14-2012 06:45 PM

SOLUTION for Ubuntu 12.04
 
This bug (tab complete not working on environment variables) is present in Ubuntu 12.04. It is a bug in bash-4.2. Applying patch bash4.2-029 solves the problem. See https://bugs.launchpad.net/ubuntu/+s...27/comments/14

centguy 12-20-2012 05:22 AM

It is also happening in Linux Mint 14 nadia. I used variables a lot and this is
starting to put me off! What is the best way to solve this ?


All times are GMT -5. The time now is 07:34 PM.