
![]() |
thedarkness
I change my working directory dependant on what project I'm working on, I came up with this script;
#!/bin/bash WD=`/usr/local/src/currentproject/` cd $WD exec /bin/bash Problem is this creates another shell. Does anyone know how to set the current working directory without opening another shell? Cheers, td cdc
I could be wrong but I think you're making this more difficult than need be.
What's creating your other shell is the exec command you're running. So don't run it. ![]() What I'm saying is that just doing: cd /my/new/dir will change your current working directory and you can access files using relative path names after you execute that line. thedarkness
Nah dude, I want a script that changes directory to the target directory and then exits leaving me in that directory.
Basically, instead of typing; cd /usr/local/src/C++/HTML/linkchecker/ or cd /var/www/html/dev/working/htmlentities/selectmodule/ I can just type; workdir and be in the target directory ready to work. Cheers, td Dbyt3r
Just take out the line d00d.. Its just the 'cd' that matters
![]() thedarkness
No, you guys just aren't getting it.
Write a program that changes directory and leaves you in that directory when it exits, try it, sounds easy, but its not. What happens is that whenever you run a script it creates a new shell, you change directory in that shell (process) and then when the script ends that process dies and you are left in the directory where you called the script from. My OP script works, but it starts an additional shell so when I want to log out I have to log out of two shells. just wondered if anyone knew a way to do it without the additional shell. Cheers, td cdc
Ah...ok.
I don't know if this will work in linuxor not, but at my old job we usedSolarisand if we "sourced" the script than any changes to the environment would be reflected in our shell.So if your script is cd /your/new/dir You should just be able to run 'source myscript.sh' and you would be in /your/new/dir Too lazy to try this on my linuxbox...but if it doesn't work it will hopefully lead you down the path to what will work in your environment.perkiset
TD this one has buggered me for quite a while. I've tried to do exactly the same thing and never had success. You and I probably do a lot of similar things... I might want to be in my
apacheconfig dir for a minute and then back to a dev dir... and have tried to write little shell scripts to do it and gave up out of frustration. Non-*nix ers won't understand it as well because DOS/Windoz DOES do what they're saying - the batch file operates WITHIN the context of the current user whereas a shell script runs in its own space... in fact, that very exercise demonstrates the profound difference between windows and *n[i|u]x quite handily.I'm glad you started this thread because I'd just love to hear from the real shell smarties about how to pull this off... /p thedarkness
Dude, cdc just gave us the answer. Source is a shell builtin in bash;
quote . filename [arguments] source filename [arguments] Read and execute commands from filename in the current shell environment and return the exit status of the last command exe- cuted from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing file- name. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched. If any arguments are supplied, they become the posi- tional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read. Looks like it does exactly what I need. You rock cdc! I've worked on HP-UX, SCO, Solaris, BSD, andLinuxextensively and never come across this. Always said I should read the bash man page from top to bottom![]() I'll give this a try and report back. Thanks again cdc, td perkiset
fishing *x rookies grumble snort. Beginners luck
![]() Fatty
I know sourcing a script solves the original question, but what ever happened to the good old alias?
$ alias workdir="cd /tmp" $ workdir $ pwd /tmp Also look at the CDPATH environment variable.. export CDPATH="/home/me/projects" from anywhere, cd proj1 will get you into /home/me/projects/proj1 if it exists Fatty perkiset
Hey fatty - welcome to the board!
/p thedarkness
@fatty: Top stuff dude, hadn't thought of the alias and never looked at the CDPATH env variable.... LOL, computing is so vast...
![]() Cheers dude, td |

Thread Categories

![]() |
![]() |
Best of The Cache Home |
![]() |
![]() |
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads