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!

customizing cd

Status
Not open for further replies.

hok1man

Technical User
Feb 16, 2008
102
Hi guys,


I would like to customize my cd:

please look the commands below
Code:
%which cd
/usr/bin/cd
%pwd
/opt/oracle
%cd ~user1
%pwd
/home/user1

%cd -
%pwd
/opt/oracle
%/usr/bin/cd ~user1 
%pwd
/opt/oracle <-- [red]see?? doesn't work[/red]

can anyone tell me what the original 'cd' command?
because I want to use alias cd to do something else.

my shell is bash and platform solaris 8
I have search man cd, they told me it's supposed to be /usr/bin/cd, but as you can see that command doesn't work

Thanks guys
 
I'm not sure why /usr/bin/cd exists to be honest, I don't see what use it has. The reason is that when you run any binary, it may change the current working directory while that binary, in this case /usr/bin/cd, is running, however as soon as it exits to the parent process the current working directory remains unchanged.

cd is normally implemented as a shell builtin. You can override it by defining an alias, e.g.

[tt]alias cd='echo'[/tt]

...however your options are very limited with aliases. Overriding it using a function doesn't appear to work, presumably because shell builtins have higher precedence than functions.

Why do you want to customise cd? Maybe your problem can be solved some other way.

Annihilannic.
 
Hi Anni,

I would like to change the title of console windows according to my current directory,

so If I have many windows/session opens, I can find out easily which one that I'm looking for, not by opening one by one to check which one...

so, what I'm thinking is...

I create script in my home directory called cd :
in that script actually saying to change directory using /usr/bin/cd after that changing the console title.. that's all. Then I include my home directory in the PATH.

so when I type cd.. I want my "script cd" which is /usr/bin/cd executed not the builtin cd.

But I searched more in the net that /usr/bin/cd and cd is actually different, which I thought they are the same..
so the cd is actually builtin from bash and ksh but /usr/bin/cd is executed command.

So, do you have any idea about my problem?

Thanks mate
 
I browse the net that I can use pushd command?
do you familiar about this command Anni?
 
Another, perhaps easier, way to do what you want is to include a string in your shell prompt that updates the xterm title. For example, this works in Korn shell:

[tt]PS1='^[]0;${PWD}^G$PWD \$ '[/tt]

Note that the red parts are control characters which you must enter in vi using Ctrl-V Esc and Ctrl-V Ctrl-G respectively.

Yes, you could also use pushd and popd, they also have the advantage of remembering your 'cd history' so you can go back again. I'm not sure whether they already include this xterm title functionality, or whether you would have to add it yourself.

Annihilannic.
 
So,

where is the best I should put your code, as my default shell is bash...

in .profile or in script somewhere?
I will try it tomorrow morning..

Cheers, mate
 
.bash_profile or .bashrc.

Note that bash has many other codes and shortcuts you can use to make fancy prompts; have a read of the relevant section in the man page.

Annihilannic.
 
Anni,

It worked but there is a little issue here.
I can't write command until the end of the edge of the window...

I can only reach probably 70% of windows size column,
I know it's related to ^[ ... ^G
because when I removed them.. my windows title doesn't work but I can write until the end column of windows back.

Is there any way to solve this issue?

cheers mate
 
Yes, that's correct, it's because bash thinks your prompt is, say, 25 characters wide, however only 15 of them are actually displayed on your terminal (the other 10 are in the xterm title). So it miscalculates your screen width. There are some codes you can use to tell it which part of the prompt to ignore when making this calculation; read the PROMPTING section of the bash man page and you'll find them.

Annihilannic.
 
Hi Anni,

I have tried to search but no luck...
same as I'm googling it...

any idea?

Thanks,
 
man bash said:
PROMPTING
When executing interactively, bash displays the primary
prompt PS1 when it is ready to read a command, and the
secondary prompt PS2 when it needs more input to complete
a command. Bash allows these prompt strings to be cus­
tomized by inserting a number of backslash-escaped special
characters that are decoded as follows:
...
\[ begin a sequence of non-printing characters,
which could be used to embed a terminal con-­
trol sequence into the prompt
\] end a sequence of non-printing characters

Annihilannic.
 
Alrite Anni,

I have tried this
Code:
PS1='^[]0;[r]W[/r][${PWD}[r]W][/r]^G$PWD \$ '
and
PS1='[r]W[/r]^[]0;[${PWD}[r]W][/r]^G$PWD \$ '

Both codes don't work?
how do I apply it?
 
sorry the code should look like this

Code:
PS1='^[]0;[red]W[/red][${PWD}[red]W][/red]^G$PWD \$ '
and
PS1='[red]W[/red]^[]0;[${PWD}[red]W][/red]^G$PWD \$ '
 
sigh...

Code:
PS1='^[]0;[red]W[[/red][${PWD}[red]W][/red]^G$PWD \$ '
and
PS1='[red]W[[/red]^[]0;[${PWD}[red]W][/red]^G$PWD \$ '
 
Where did the W's come from? They are supposed to be \ characters.

Try:

Code:
PS1='[COLOR=red]\[[/color]^[]0;[${PWD}^G[COLOR=red]\][/color]$PWD \$ '

Annihilannic.
 
I was half sleep sorry.
yeah.. I have tried yours and it worked, Great thanks.

Anyway, how do you know about ^[ and ^G thing?
I have searched in man bash and couldnt find it.
Just interesting.. how you know so much :)
 
Annihilannic said:
Overriding it using a function doesn't appear to work, presumably because shell builtins have higher precedence than functions.
The precedence is the other way around, which causes infinite recursion when you try to call cd from within a function called cd. And you can get around it by using [tt]builtin[/tt] to force the use of the built-in command. e.g.
Code:
function cd { builtin cd "$1" && ls; }
 
Thanks for the tip chipperMDW. Serves me right for testing a bash question under ksh [blush]

In ksh88 on HP-UX 11.11.

[tt]$ function cd
> {
> echo hi
> }
$ cd
$ cd /tmp
$ pwd
/tmp
$[/tt]


Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top