Change directory command (cd)

What's wrong with this script?????

Please help!!!

!#/bin/sh

echo "please enter dir"
read input
cd $input

I'm trying to make a simple script to change dir. But does not work.

it stays at the same dir where i run the script.

What did i do wrong??

what is the output???
what happens after you run the script?

try doing an echo $input before cd $input....

you must be specifying only the directory name. Are you giving the whole path?
You need to give the whole path with your current code in the script.

This is because the script runs in a subshell, and once the script exits, it comes to your working dir where you were.

//Jadu

i did give the whole path..

e.g.: i run my script from /home/blah/test

when i run the script..

i enter /home/blah as the input

the input shows "cd /home/blah"

when the script finishes, i run "PWD" command and i'm still at /home/blah/test instead of /home/blah

It is absolutely working fine but as it is not changing the current directory you are saying it is not working. Try to run your script using source command. eg source <script name> <argument>

When u run the script, at time it creates a separate shell and do the operation in that shell and when programm terminates the shell also got killed. If you still have doubt, please let me know.

HI Jadu,

So what should i do if the script runs as subshell?

It means i would never get it working?

arre sahi ...........
how could i forget..........
allu have to do is put a '.' and then a space before the cd command.
that shld work just fine :smiley:

No as Siba told, it would work.

do
source <your script> to execute

//Jadu

$^^%&%&#

it complains my $input...

It says "UNDEFINED VARIABLE"

I don't think running cd in a shell script will actually jump to the directory. The directory path will still be static to your current working directory even after jumping

when u run your file change1.sh(if dats the name of ur file)
then all u have to do is:

$ . change1.sh

the directory will change.

i can't...

i received "undefined variable" error for the the input

hey why don't u jst post all the lines that u executed along with the o/p u see on ur screen... line by line....
show us what exactly u type and what happens.
if theres' an undefined variable then there may be a spelling mistake in the variable "input" that u used...

correct maybe your input directory is wrong.

One other way that works on my system is

$ source change.sh
bash: !#/bin/sh: No such file or directory
please enter dir
/home

nua7@XX0703328304 /home
$

I made a script using VI editor called "script", save the file at /home/blah/test

#!/bin/sh

echo "Please enter the dir"
/home/blah

read input
echo $input
/home/blah

cd $input

go back to prompt.
and when i do "PWD" command, i'm still at /home/blah/test

when i run the script using

# . ./script

please enter dir
/home/blah
input: Undefined variable.

#

what do u type in at the read input line?
does the directory exist???
in short:
carry out the following steps:

  1. create a directory
    $ mkdir varun
  2. create a file change.sh and type in the nxt 2 lines
    $ vi change.sh
    ~echo "enter a directory"
    ~read dir
    ~cd $dir
    ~
    ~
  3. save the file and exit.
    :wq!
    "change.sh" 3L, 48C written
  4. Now run this file
    $ . change.sh
    enter a directory:
    varun
    varun$

This thread has carried on too far now.
Just follow the steps above.
If it doesn't work then lets assume that u r using a customized version of unix which may have some differences.....
or u might not be disclosing all the relevant info thats needed :slight_smile:

what do u type in at the read input line?
does the directory exist???
in short:
carry out the following steps:

  1. create a directory
    $ mkdir varun
  2. create a file change.sh and type in the nxt 2 lines
    $ vi change.sh
    ~echo "enter a directory"
    ~read dir
    ~cd $dir
    ~
    ~
  3. save the file and exit.
    :wq!
    "change.sh" 3L, 48C written
  4. Now run this file
    $ . change.sh
    enter a directory:
    varun
    varun$

This thread has carried on too far now.
Just follow the steps above.
If it doesn't work then lets assume that u r using a customized version of unix which may have some differences.....
or u might not be disclosing all the relevant info thats needed :slight_smile: