Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Piping to a cd command

Status
Not open for further replies.

SteveR77

Programmer
Sep 18, 2000
813
US
This is a bit of a puzzle to me.

If I do the following:

1 - Change to the /etc directory

2 - echo $HISTFILE | cd

I would expect one of two things to follow.
1 - I get changed to the directory that HISTFILE is holding
2 - I get changed to my home directory on my system

The problem that I am having is the command does neither. It leaves me in the /etc directory.

This is under Red Hat Linux. I believe the version is 5.2 ( I know it's old but piping should work)

Thanks in advance.

Steve

 
Hi Steve,
do you have $HISTFILE variable in your environment?
try running that it self.
#echo $HISTFILE

In order to run two commnad one by one use ;
#echo $HISTFILE;cd

Sachin
 
patel,

Yes the HISTFILE variable is defined. The echo$HISTFILE; cd shows the correct information for the location of the HISTFILE and cd will place me back in my home directory as it should.

I checked and doubled checked this by running xtrace and by using the syntax that you gave me.


As I said patel. This one is a bit of a puzzle, because I have used the syntax in other shells and other versions of Linux without any problems.
 
I think the problem is that the cd command isn't designed to accept piped input in that way. Why don't you just do cd $HISTFILE

Greg.
 
Greg,

I know that cd $HISTFILE will work and I suggested it as a solution. I can live with that answer, but I also know that in some cases it does work.

I just need to find a reference that states that the cd command does not or will not accept piped input, and I can not seem to find it anywhere.

Thanks for the follow up and I'll see if I can track down the reference.
 
OK, I think I have solved the mystery on this one.

This came from the HP-UX man pages. So I'm going to make a generalization that all SVR4 Unix off shoots work this way.

Notes: Some commands work well as filters, such as sort, wc, file, grep. Other commands will not work as filters; these include cd, ls, pwd, date and who. The main
criterion for determining if a command can be used as a filter is whether it will accept the results of another command (the standard output) as its standard input.

There is no way to redirect any standard output as input to those commands listed above (cd, ls, pwd, date and who); each of them will accept only the default standard
input from the system, so they will not work as filters.

This is basically what grega said but with a bit more detail as to why.

If anyone finds out that the BSD off shoots for the most part behave this way too please post that information.


The bottom line appears to be if you want to apply a change directory command passing it a variable do this:

cd $varname
 
What you probably want is something like (for ksh):

cd $(dirname $HISTFILE)

OR a more convoluted:

eval $(dirname $HISTFILE | xargs echo cd)

Or if you really want to see the dirname of the dir you are changing to:

eval $(dirname $HISTFILE | xargs -i echo echo {} \; cd {} )

Or why not just create a function like:

function cdh() {
theDir=$(dirname $HISTFILE)
echo $theDir
builtin cd $theDir
}

It depends what you are trying to do ;-)

Cheers, NEIL
 
Thanks Neil.

I'll keep all those in mind. The root of the question was based on attempting to use piping.

B-)

Cheers, Steve
 
Neil's first solution does use a pipe (internally) by the way.

If you apply it to your example in your first post you might get something like this:

cd $(echo $HISTFILE) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top