Reputation:
This may be too generic a question as is but... I am stumped by trying to move through the directories from within a shell script. I'm not a *nix power user, but I am comfortable working through the command line for most tasks. I'd like to call a script that can move 'me' to a directory instead of just the script process similar to the following:
prompt:> goto lit
where goto
is an alias -> goto='./goscript'
and
goscript
has some simple code in such as:
cd /path to work dirs/lit/user dir
(assuming each user has a directory inside /lit)
I've avoided this issue myself by setting my personal alias' to move to the desired directory, run a script, then return to the original directory. This question was brought to me by a co-worker who uses a similar method, but wanted to make the process more generic so we don't need to create every single alias we need. I thought this would be an easy problem to solve, but I'm stumped as I don't really have a great deal of shell scripting experience ...as of yet.
Upvotes: 6
Views: 2282
Reputation: 7493
Even better than using an alias as others have described, check out the CDPATH
variable! It's basically equivalent of the PATH
functionality, but applied to the cd command.
For example, if I define my CDPATH
as $CDPATH:${HOME}/subdir
, and ~/subdir
contains another directory, subsubdir
, then I can simply execute:
cd subsubdir
from any directory, and navigate the the path as expected.
Here's some more specifics:
http://www.caliban.org/bash/#bashtips
To set the CDPATH
variable, add a line to your .bashrc
, such as
export CDPATH=$CDPATH:${HOME}/subdir
Upvotes: 9
Reputation:
I wasn't a member when I asked this (ironically my anonymous login question has higher flair than my official login questions combined), so I can't close it now - But- The Remo D. et al. answer would be the one that led to the working solution we needed. If anyone with mod powers can close this & select that as the accepted answer, i'd apprediate it.
Upvotes: 1
Reputation: 11782
I tend to have a couple dozen aliases in my .rc that look like this:
alias work='cd /home/foo/work'
alias rails='cd /home/foo/rails'
alias assets='cd /home/foo/rails/assets'
etc.
If that isn't going to work for you, you might be able to use the debug hooks in bash to replace the command line in-place before it is executed - if the 'goto' keyword is the first thing on the line. This will involve googling around for 'trap' in bash, but be forewarned, such incantations do not behave the same on all systems. I wrote up one such experience a few months ago, and you're welcome to the results.
Upvotes: 0
Reputation: 16522
To execute a script in the same environment of your prompt you have to invoke it with .
Say you have the script named up:
cd ..
if you invoke it with . you get:
$> pwd
$> /home/users/rd/proj
$> . up
$> pwd
$> /home/users/rd
Upvotes: 4
Reputation: 35934
You can create a function that is called goto (or whatever) and make sure it is defined in your .bashrc (or you can "source" it from your current shell):
function goto {
# the "$USER" part will expand to the current username
# the "$1" will expand to the first argument to the function ("goto xyz" => $1 is "xyz")
cd /some-path/lit/$USER/$1
}
Put this in ~/.bashrc or in a separate file and call "source the-file" from your prompt then you can call the function just like any other program:
prompt> goto folder
cd /some-path/lit/your-user/folder
Upvotes: 5
Reputation: 13180
Each script has its own idea of the current working directory. "cd" is not a process but a shell build-in.
The way to do what you want is to create an alias. In csh it will be something like:
alias goto 'cd /path_to_work/\!*/user_dir'
sh/bash have similar alias facilities
Upvotes: 0
Reputation: 11834
You can accomplish some simple stuff like this using an alias. For example, I have some aliases that put me into a workspace environment and configure variables for our Makefile system. Here's a real alias I am using as an example. The '&&' will continue executing the commands until one of them fails--in simple scenarios like this, no clean-up is needed, since failure isn't likely.
alias main1='cd ~/code/main1 && export TOP=`pwd` && export DEBUG=1'
If you want to develop a shared library of aliases amongst your co-workers, you can share them over NFS and source the file to be included.
Upvotes: 0
Reputation: 577
You can't use cd in a bash script. You could alias the cd and path though.
alias goto='cd /path_to_work/usr/dir'
UPDATE: you would put that line in your .bashrc file and then do
source ~/.bashrc
to create the alias.
Upvotes: 0
Reputation: 182850
You can't. The script has its own copy of the environment, and so it can't change the login shell's environment.
Upvotes: 2