The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash Trap Problem? jsabino Shell Programming and Scripting 3 11-17-2008 01:45 PM
Bash problem edgehead Shell Programming and Scripting 3 11-15-2008 04:34 AM
bash array problem yagnesh Shell Programming and Scripting 1 08-19-2008 03:48 AM
Bash: bad substitution problem...pls help! xfouxs UNIX for Dummies Questions & Answers 1 11-23-2007 05:48 PM
Bash while loop problem Kweekwom Shell Programming and Scripting 5 07-23-2007 01:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-11-2009
usfish usfish is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 2
problem about '..' in bash

There is an interview question about UNIX bash:

Some shells like bash try to make '..' always work propery, namely, from home directory, cd ../$USER will place you in your home directory. Does bash always get this behavior correct?

Anyway can provide any example that bash doesnt work well?

Thanks!

Last edited by usfish; 02-11-2009 at 02:34 AM..
  #2 (permalink)  
Old 02-11-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
Quote:
Originally Posted by usfish View Post
There is an interview question about UNIX bash:

Some shells like bash try to make '..' always work propery, namely, cd ../$USER will place you in your home directory. Does bash always get this behavior correct?

Anyway can provide any example that bash doesnt work well?

"cd ../$USER" will not place you in your home directory unless you are in a directory on the same level as your home directory.

You may be thinking of:


Code:
cd ~user

That will not work with a variable unless you use eval:


Code:
eval "cd ~$USER"

To go to your home directory, use cd with no directory.
  #3 (permalink)  
Old 02-11-2009
usfish usfish is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 2
Quote:
Originally Posted by cfajohnson View Post

"cd ../$USER" will not place you in your home directory unless you are in a directory on the same level as your home directory.

You may be thinking of:


Code:
cd ~user

That will not work with a variable unless you use eval:


Code:
eval "cd ~$USER"

To go to your home directory, use cd with no directory.
Hi, thank you for your prompt response. I missed that condition. It is from the home directory and you type "..", and they meant to ask about ".." instead of $USER I guess.

I think there might be chance that bash fails on symbolic link?
  #4 (permalink)  
Old 02-11-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
Quote:
Originally Posted by usfish View Post
Hi, thank you for your prompt response. I missed that condition. It is from the home directory and you type "..", and they meant to ask about ".." instead of $USER I guess.

I think there might be chance that bash fails on symbolic link?

What do you mean by "fail"?

If you want to cd to the physical parent directory, use the -P option:


Code:
cd -P ..

  #5 (permalink)  
Old 02-11-2009
larryv larryv is offline
Registered User
  
 

Join Date: Feb 2009
Location: Cambridge, MA, USA
Posts: 1
hmmm

Quote:
Originally Posted by usfish View Post
There is an interview question about UNIX bash:
Funny, that looks an awful lot like one of the extra credit questions on my latest 6.033 (MIT Computer Systems Engineering) homework.

Let's clarify the problem.

Say we have some arbitrary directory at /some/long/path/name/, and say that our home directory is /home/me/.

Now, we do this:

Code:
ln -s /some/long/path/name /foo/bar
ln -s /home/me /foo/baz

So now, assuming that /foo does not contain two directories called bar and baz, we now have two symbolic links, /foo/bar and /foo/baz, that link to /some/long/path/name and /home/me, respectively. If we perform the following:

Code:
bash
cd /foo/bar
cd ../baz

we should end up in /foo/baz, which is really /home/me, because bash traced .. back up to /foo, not to /some/long/path/, as tcsh does (if I'm not mistaken; if not, it used to).

Ultimately, the question is, does bash always get this behavior correct? When cd-ing to another directory via a symlink, does cd .. always get you back to the directory that contains the symlink?

Last edited by larryv; 02-11-2009 at 06:50 PM.. Reason: clarify final sentence
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0