wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes Question about Bash Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Question about Bash" Watch "Question about Bash" New topic
Author

Question about Bash

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

I'm using Bash on Ubuntu Linux. When I use tab to complete an environment variable, sometimes it escapes the $, and other times it doesn't.

For example,

> echo $JAVA [tab]
becomes
> echo $JAVA_HOME

That's good. However,

> cd $JAVA[tab]
becomes
> cd \$JAVA_HOME

which isn't what I want. Is there anyway to control this behavior? I don't remember hitting it before.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

I also encountered the same issue.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15632
    
  15

Bash is sensitive to context. When you enter an "echo" command, any text at all is possible, but for execution paths and other filesystem contexts, the rules are a bit different. What it thinks you're asking for is an actual directory named "$JAVA_HOME", so it escapes the "$".

You might also have noticed that if you tab-complete an environment variable, bash will substitute its actual current value, That is,

cd $JAVA_HOME[tab]

might turn into

cd /usr/java/jdk1.6


Customer surveys are for companies who didn't pay proper attention to begin with.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

So it sounds like I'm stuck with either downloading the Bash source, making my own modifications, and recompiling ... or just learning to live with it?

I guess I'll live with it. I can always define some handy aliases to reduce the pain.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Try followed by

HTH,
- Anand

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Thanks Anand! I didn't know about the shopt command. It doesn't really do what I want, since I still have to type out "JAVA_HOME", but it's a good thing to add to my tool belt nonetheless.

Tim, cd $JAVA_HOME[tab] becomes cd \$JAVA_HOME for me. What do you mean it "might" turn into the value of the variable?
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Greg Charles wrote:Thanks Anand! I didn't know about the shopt command.

You are welcome.

Greg Charles wrote:It doesn't really do what I want, since I still have to type out "JAVA_HOME", but it's a good thing to add to my tool belt nonetheless.

Tim, cd $JAVA_HOME[tab] becomes cd \$JAVA_HOME for me. What do you mean it "might" turn into the value of the variable?

The behaviour you see, the behaviour I have (which completes the environment variable) and the behaviour Tim has are all different because of how the distro configures the auto-completion out-of-the box.

In an earlier message, Greg Charles wrote:So it sounds like I'm stuck with either downloading the Bash source, making my own modifications, and recompiling ... or just learning to live with it?

Most likely not. Suggest you go through the man page for bash, especially the section "Programmable Completion" and hopefully you should be able to figure out how to tweak /etc/bash_completion (IIRC -- the file name and location might be different depending upon the distro).

HTH,
- Anand
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Well, I took a look at the bash man page. I have to say the word that jumps first to my mind is byzantine. It's nice that it is customizable, but it's just a shell after all. I don't want to spend my lifetime learning how to configure it. In any case, I found a guy who reported this issue as a bug against a Debian release about five years ago. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=278295

He suggests a fix to /etc/bash_completion



Even working from the command backwards through the man page, I still can't really figure out what these do. I think by the -, he means to delete those lines, and + means add. So, basically, change $filenames to $dirnames. That does, in fact, fix my problem, but with a side-effect. Right now if I type part of a directory and hit tab it expands it and adds a slash at the end. That very useful for navigating into deep directory structures that only have one directory at each level. That's often the case with Java packages so, if I want to get to com/mycompany/package, I can just hit: > cd com{tab][tab][tab] After making the fix, the slash isn't added anymore, even though the expansion seems to still understand I'm only interested in directories.

I'm sure there must be a way to get both things to work right. Maybe I'll keep playing with it. For now though, I'll just make aliases to get to $JAVA_HOME and some other directories quickly.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Bash is indeed "just a shell after all", but shells are not simple beasts. They are interpreters for a programming language (shell scripts are programs written for these interpreters). Bash and other modern shells allow an incredible degree of customisation so you don't have to look at and tinker with its source code (which, I can assure you, is NOT trivial).

Also, the manual pages are poor tutorials (kind of like using a dictionary to learn a language). I can empathise that you find bash's man page overwhelming.

Very nice that you were able to find a web-page that dealt with the same problem (curious to know what were your search terms). As for the + and -, you may want to Google for 'context diff' and 'patch' (BTW, your hunches were spot on).

best,
- Anand
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

I hope the source code is written better than the man page, at least the section on the complete command! I don't totally agree that bash is a programming language. It has relatively few built-in commands, so really it's just a framework for running external commands. It's useful as a scripting language only insofar as it's easy to use. Otherwise you might as well use Perl or Python or even C or Java.

I admit expansion is not completely trivial, because you have to decide based on context whether you are expanding an executable, file name, directory name, or shell variable. On the other hand, it's not rocket science. Reading further into that bug report reveals the same issue has been reported periodically over the years, and the final conclusion is that it can't be fixed because of a weakness in Bash. That seems a bit circular. A bug can't be fixed because it's a bug? To me that says over-engineered. YMMV

Ah, I think I googled for: bash cd command escapes $
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Hm. I allways struggle with jar and zips.

The only tool I build zips with is jar, because I know it's syntax. But unzipping from shell, the autocompletition refuses to show any zipfile.

But I'm guilty not searching the web for an answer, so don't work for me, I don't deserve it.


http://home.arcor.de/hirnstrom/bewerbung
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Haha, it's nice to know it's not only me. However, you do provide a reason why the expansion rules are so configurable. I can see how it would be useful for only jar files to show up as possible parameters to the jar command. Maybe I was too quick to judge!

Anyway, I'm sure it's possible to make jar look at both jars and zips, but I don't know how. I did learn something in my adventures through the man page though. You can use "complete -r jar" to take off all the clever expansion rules from the jar command. That means all files will show up in the expansion: jars, zips, gifs, mp3s ... everything. Not directories though I think. If that's better for you, just throw that line at the end of your .bashrc file and you'll be all set.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Bash
 
Similar Threads
tomcat and java_home
JAVA_HOME problem
Problem in Configure JAVA in Linux
problem in starting tomcat
how to set catalina home