1

i want to make a script (to) that makes it easier for me to enter folders.

so eg. if i type "to apache" i want it to change the current directory to /etc/apache2.

however, when i use the "cd" command inside the script, it seems like it changes the path WITHIN the script, so the path in the shell has not changed.

how could i make this work?

1

4 Answers 4

5

Use an alias or function, or source the script instead of executing it.

BASH FAQ entry #60.

Sign up to request clarification or add additional context in comments.

Comments

4

use a function

to_apache(){
  cd /etc/apache
}

put in a file eg mylibrary.sh and whenever you want to use it, source the file. eg

#!/bin/bash
source /path/mylibrary.sh
to_apache

Comments

2

As Ignacio said, make it into a function (or perhaps an alias).

The way I tend to do it is have a shell script that creates the function - and the script and the function have the same name. Then once at some point in time, I will source the script ('. funcname') and thereafter I can simply use the function.

I tend to prefer functions to aliases; it is easier to manage arguments etc.

Also, for the specific case of changing directories, I use CDPATH. The trick with using CDPATH is to have the empty entry at the start:

export CDPATH=:/work4/jleffler:/u/jleffler:/work4/jleffler/src:\
/work4/jleffler/src/perl:/work4/jleffler/src/sqltools:/work4/jleffler/lib:\
/work4/jleffler/doc:/u/jleffler/mail:/work4/jleffler/work:/work4/jleffler/ids

On this machine, my main home directory is /work4/jleffler. I can get to most of the relevant sub-directories in one go with 'cd whatever'.

If you don't put the empty entry (or an explicit '.') first, then you can't 'cd' into a sub-directory of the current directory, which is disconcerting at least.

2 Comments

I don't have any trouble with cd into a subdirectory without a leading ":" in CDPATH. (Bash 3.2 or 4.0)
@Dennis: well, it could be a piece of folk lore I picked up from Korn Shell on Unix vs bash; it most certainly was a problem when I first started using it with ksh - but I've not validated whether it is a problem in bash (since what I have works fine in bash).
0

Ignacio Vazquez-Abrams gave a link to what probably answers the question, although I didn't really follow it. The short answer is to use either "source" or a single dot before the command, eg: . to apache

But, I found there are down problems to this if you have a more complicated script. It seems that the original script filename variable ($0) is lost. I see "-bash" instead, so your script can't echo error text that that would include the full filename.

Also, you can't use the "exit" command, or your shell will exit (especially disconcerting from ssh).

Also, the "basename" function gives an error if you use that.

So, it seems to me that a function might be the only way to get around some of these problems, like if you are passing parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.