"cd" doesn't take effect when running in script, but does in console

This is a discussion on "cd" doesn't take effect when running in script, but does in console within the Linux Programming forums, part of the Platform Specific Boards category; I am sure it is a silly question but here is what I am doing: # pwd /root/bin # echo ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    Talking "cd" doesn't take effect when running in script, but does in console

    I am sure it is a silly question but here is what I am doing:

    # pwd
    /root/bin

    # echo "cd /" > foo

    # sh foo

    # pwd
    /root/bin


    How can I make foo script file to change the directory to "/"


    Thanks,
    Rami

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    I hope your browser isn't running as root as well.

    You need to 'source' the script if you want it to take effect in the current environment.

    So rather than
    # sh foo

    You would do
    # source foo
    or
    # . foo
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    2
    thx salem. it worked

    I am the root.
    what do you mean "I hope your browser isn't running as root as well."

    --Rami

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    As in, you lazily did
    # firefox &

    Now, your browser is running as root, which means anything which manages to compromise your browser will also be running as root. At that point, it's "bye bye" machine, and you're looking for your install disks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running a console application
    By maxorator in forum C++ Programming
    Replies: 4
    Last Post: 10-03-2005, 04:23 AM
  2. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  3. How to tell when internal program is done running
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 03-21-2005, 09:09 PM
  4. Problem with windows console when running.
    By RealityFusion in forum C++ Programming
    Replies: 5
    Last Post: 08-07-2003, 08:56 PM
  5. Running 'exec' twice in one UNIX shell script
    By Zughiaq in forum Tech Board
    Replies: 2
    Last Post: 05-03-2003, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21