LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-12-2008, 09:52 PM   #1
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Rep: Reputation: 0
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

Last edited by armandino101; 01-12-2008 at 10:52 PM. Reason: typo
 
Old 01-13-2008, 12:49 AM   #2
aus9
Guru
 
Registered: Oct 2003
Posts: 5,056

Rep: Reputation: Disabled
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.
 
Old 01-13-2008, 04:52 PM   #3
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
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?
 
Old 01-14-2008, 12:41 AM   #4
aus9
Guru
 
Registered: Oct 2003
Posts: 5,056

Rep: Reputation: Disabled
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.
 
Old 01-14-2008, 10:14 PM   #5
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
Hey, no worries. I appreciate your help.

Mods, if you feel there's a more appropriate forum for this question, please move it there
 
Old 01-15-2008, 06:16 PM   #6
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
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.
 
Old 02-12-2008, 07:35 PM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
Talking 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?
 
Old 02-13-2008, 09:55 PM   #8
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
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
 
Old 02-14-2008, 12:23 PM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
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 View Post
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.
 
Old 01-25-2012, 10:29 AM   #10
revprez
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 08-14-2012, 06:45 PM   #11
nacorn
LQ Newbie
 
Registered: Aug 2012
Location: California
Posts: 1

Rep: Reputation: Disabled
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
 
Old 12-20-2012, 05:22 AM   #12
centguy
Member
 
Registered: Feb 2008
Posts: 394
Blog Entries: 1

Rep: Reputation: 35
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 ?
 
  


Reply

Tags
autocomplete, bash, environment, solution, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ignoring Environment Variables when Starting a BASH Script Garnett Linux - Software 3 09-21-2007 01:00 PM
how to set environment variables in bash profile praveenkorukonda Linux - Newbie 1 02-21-2007 11:40 PM
Environment variables for bash htarko Programming 1 09-13-2006 03:57 PM
Bash script environment variables mbjunior99 Suse/Novell 4 12-28-2005 12:40 AM
bash auto-complete behaviour changes acid_kewpie Linux - Software 5 10-26-2002 01:32 PM


All times are GMT -5. The time now is 02:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration