Results 1 to 8 of 8

Thread: shell variable: PWD

  1. #1
    Join Date
    Aug 2009
    Beans
    109

    shell variable: PWD

    changing the value of variable name PWD show the changed current directory but actully it is in the same directory..
    Code:
    piyush@piyush-machine:~$ cd try
    piyush@piyush-machine:~/try$ ls
    f  file  mkdir t  try
    piyush@piyush-machine:~/try$ PWD="/donot/exist"
    piyush@piyush-machine:/donot/exist$ ls
    f  file  mkdir t  try
    piyush@piyush-machine:/donot/exist$
    Can u explain its mechanism..??

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: shell variable: PWD

    PWD is a bash variable which you should usually not mess with.
    It is set when you change working directory but not vice versa.

    it is apparently also used to display the working directory so you get a wrong display when you change it.

    use cd to change the working directory

  3. #3
    Join Date
    Aug 2009
    Beans
    109

    Re: shell variable: PWD

    can you explain this too...just giving echo leaves 1 line..so the second command should leave two line but it is still leaving one..why???
    (so much of doubts today....)
    Code:
    piyush@piyush-machine:~$ echo 
    
    piyush@piyush-machine:~$ echo `echo`
    
    piyush@piyush-machine:~$

  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: shell variable: PWD

    backquotes seem to strip ending newlines of output:
    var=`ls`
    echo -n $var

    this has no ending newline, although normal ls does
    seems reasonable as backquotes are often used like that, and the ending might disturb further handling of the variable, and is just needed for more visually pleasing output

    some other weird behavior I just found:
    var=`ls -1`
    echo -n $var #no newlines!
    echo -n "$var" #newlines, but trailing newline stripped
    Last edited by MadCow108; January 25th, 2010 at 02:41 PM.

  5. #5
    Join Date
    Aug 2009
    Beans
    109

    Re: shell variable: PWD

    echo have new line problem from history...
    May be someone else clarify it...

  6. #6
    Join Date
    Aug 2008
    Location
    Queensland, Australia
    Beans
    167
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: shell variable: PWD

    edit: nevermind I mistyped.
    Last edited by Garratt; January 25th, 2010 at 04:51 PM.

    for(i=1;i==1;i+i-i)
    cout << "^G nothing to see here";

  7. #7
    Join Date
    Aug 2009
    Beans
    109

    Re: shell variable: PWD

    Quote Originally Posted by Garratt View Post
    edit: nevermind I mistyped.
    A lot of people can't.....but none of u need to mention it here.

  8. #8
    Join Date
    Aug 2009
    Beans
    109

    Re: shell variable: PWD

    Quote Originally Posted by MadCow108 View Post
    backquotes seem to strip ending newlines of output:
    var=`ls`
    echo -n $var

    this has no ending newline, although normal ls does
    seems reasonable as backquotes are often used like that, and the ending might disturb further handling of the variable, and is just needed for more visually pleasing output

    some other weird behavior I just found:
    var=`ls -1`
    echo -n $var #no newlines!
    echo -n "$var" #newlines, but trailing newline stripped
    well now i can put a bit more light over this..
    echo is a command and every command accepts arguments.Shell removes extra spaces,tabs and new lines while accepting arguments. So in this case
    echo -n $var #no newlines!
    all new lines area removed..


    But in second case
    echo -n "$var" #newlines, but trailing newline stripped
    due to quoting the value of $var will be treated as single argument and all its \n will be there in output....

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •