script to change shell's pwd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to change shell's pwd
# 1  
Old 12-20-2011
script to change shell's pwd

Hi there,

i was presented with a challenge that is beyond my current shell knowledge: how can you have a script that executed interactive will change your current working directory?

Example (under MacOS):
1. start Terminal and my current working directory is my home folder
2. execute a script that, when completed, will change my shell's working directory to something else....

Is this doable?
# 2  
Old 12-20-2011
Yes. IF you source a script it runs in the conext of the current process rather than as a child

two ways
Code:
# bash source command
source myscript.sh
# standard for other shells
. myscript.sh

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-20-2011
jim,

your suggestion does work but it's not quite i had in mind....
I'll provide additional details: let's assume that i want to change the behaviour of built-in ls command, where by typing:
ls folder_name
instead of listing the content of that folder, it will list the content AND change directory to that folder.

there must be a way to do it, isn't it? Smilie
# 4  
Old 12-20-2011
If you take the question literally, then no. The shell has to change its own directory, nothing from the outside can force it to change. Any external program will have its own, separate, current directory and changing it won't change the shell's.

You can tell the shell to execute a script internally, though, which is how jim's solution works. The effect of a sourced script is the same as if you'd typed it into your shell.

---------- Post updated at 12:19 PM ---------- Previous update was at 12:16 PM ----------

Quote:
Originally Posted by gigagigosu
I'll provide additional details: let's assume that i want to change the behaviour of built-in ls command, where by typing:
ls folder_name
instead of listing the content of that folder, it will list the content AND change directory to that folder.

there must be a way to do it, isn't it? Smilie
"built-in" has a specific meaning in the context of shell languages, and ls is not a built-in. It's an external program. Quite a few things you use in the shell probably aren't built-ins.

If you have bash or ksh, you could do it with a function I suppose:

Code:
function lcd
{
        ls "$1" && [ -d "$1" ] && cd "$1"
}

lcd folder

The [ -d ] part is so it doesn't bother cd-ing into non-folders.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with pwd for script run by double click on script (MacOS High SIerra)

Hello, I have the following script that just archives and clears some log files. #!/bin/bash # script: archive_logs_and_clear # add date to logfile names and copy archive directory # clear logs # change to script directory cd ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

2. UNIX for Dummies Questions & Answers

Pwd script

when user types "pwd.sh joe" at command line, it generates pwd as abc123 and when user types "pwd.sh jane" at command line, it generates pwd as abc123. pwd will change every 90 days. my pwd file will be always abc123 until i change joe/jane/bob pwd manually. (3 Replies)
Discussion started by: lawsongeek
3 Replies

3. Shell Programming and Scripting

getting pwd of script

how can i get the absolute path of whatever directory a script and/or command is in when it is run? i want to know the directory. say for instance, if i were to run the "who" command, i want to know exaclty where the who command is located. if a user ran a script, i want to know where there... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. SuSE

ldap client_forcible pwd change

Hi, I have configured ldap client on openSUSE 11.3 with yast2 config. Since I am able to get list of all users through getent, it seems configuration done properly.But while logging in with ldap id its prompting for password change. login as: testuser Using keyboard-interactive... (1 Reply)
Discussion started by: tuxian
1 Replies

5. AIX

sync samba pwd with aix5.3 pwd

currently, my samba login works just fine. i want my clients to use aix5.3 account to login to samba so they don't have to change samba pwd and aix pwd. i googled, and vi /usr/lib/smb.conf per some of knowledge base, but i could not get to work. aix5.3 and samba 3.0.24.0 thanks in advace..... (2 Replies)
Discussion started by: tjmannonline
2 Replies

6. Homework & Coursework Questions

implementing mkdir, chdir, mv, pwd inside a shell !

1. The problem statement, all variables and given/known data: need to implement mkdir, chdir, mv, pwd given a shell.cpp directory.cpp and some other files this shell missing these commands, and i need to implement them inside the shell 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: evantheking
0 Replies

7. Shell Programming and Scripting

Change the Windows Batch script to UNIX shell script.

Hi, When I run the below script in UNIX it's throwing syntax errors. Actually it's a windows batch script. Could anyone change the below Windows Batch script to UNIX shell script... Script: REM :: File Name : Refresh_OTL.bat REM :: Parameters : %1 - Region REM :: : %2 - Cube Type REM ::... (5 Replies)
Discussion started by: tomailraj
5 Replies

8. Solaris

How to change pwd during logging on ftp server?

Dear All, Could I change password while login in to ftp server(solaris 10)? I tried to use fileZilla and command prompt(window) to change my password but It can't. Do you have any suggestion ? Ps. I can't telnet and ssh to the server because of poicy for ftpuser. Thank in advance (3 Replies)
Discussion started by: unitipon
3 Replies

9. Shell Programming and Scripting

pwd & cd commands not working in shell script

Here is my script #!/bin/bash pwd cd /var/lib/pgsql Both "pwd" and "cd" are not executed is there any other way i can change the current working directory to /var/lib/pgsql pls help! (9 Replies)
Discussion started by: perk_bud
9 Replies

10. UNIX for Dummies Questions & Answers

echo $SHELL, $PWD and etc.

hi, this echo $SHELL will give the shell name.. how to get the other list of variables (besides SHELL) values? and also, different shells have different variable names (example SHELL) (10 Replies)
Discussion started by: yls177
10 Replies
Login or Register to Ask a Question