APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS













Linux Skills Test

Copyright 2002,2003 A.P. Lawrence


Answer


/etc

Comments /Tests/Linux/e0613.html


Thu Jul 28 13:08:47 2005   anonymous

i doubt this answer


Thu Jul 28 13:39:26 2005   TonyLawrence

Why don't you try it? It is correct unless you lack rights, etc.


Sun Aug 7 23:31:08 2005   anonymous

The correct answer is /tmp. You are using ls on the directory /etc. />


ls /etc list what is in that directory. Your question refers to the current
working directory which would be /tmp. My suggestion is to ls /tmp and ls
/etc. This will show you what each has in them. Then follow your diections:




cd /

cd /tmp

ls /etc

cd $_



You will see your CWD is /tmp.


Mon Aug 8 09:17:21 2005   TonyLawrence

Nope.



Please try it yourself. You'll see that it is /etc. I think you are thinking
of $OLDPWD or ~-, not $_




Wed Sep 7 16:19:12 2005   anonymous

yup, goes to /etc here.


Wed Oct 5 18:00:57 2005 /etc  anonymous

You end up at /etc.

Don't forget to make sure you are in Bash.

Dan


Wed Feb 8 12:13:52 2006   nipoonvasavada

well the answer provided is right .but let me add one thing Before typing
cd $_, if we execute pwd command then cd $_ command will give error.Thank
You.


Wed Feb 8 12:28:24 2006   TonyLawrence

Of course. Doing "pwd" set $_ to "pwd" - so unless you have a "/tmp/pwd"
directory, the "cd $_" will fail.



I think people are not understanding that this question is about the value
of $_, not the behavior of cd.






Tue Apr 25 09:35:02 2006   anthonyc

The answer to the question is correct, $_ set /etc as the pwd.


Fri Apr 28 18:13:56 2006 Of course it's /etc.... (from the BASH ref manual)  anonymous

It's right out of the manual........



If the `-k' option is set (see section 4.3 The Set Builtin), then all parameter
assignments are placed in the environment for a command, not just those
that precede the command name.



When Bash invokes an external command, the variable `$_' is set to the
full path name of the command and passed to that command in its environment.




So the ls sets $_ to /etc and sets you up for the cd. If you do a 'set'
or other command in between, yes, you'll get an error.



Pretty simple stuff really, once you rtfm.




Tue May 2 03:57:25 2006   anonymous

hello what is pwd command?


Tue May 2 10:06:56 2006   TonyLawrence

Print Working Directory






Fri Aug 4 15:42:38 2006   anonymous

i think the correct answer is /tmp


Fri Aug 4 22:20:50 2006   TonyLawrence

Well, you "think" wrong. Try it.


Wed Aug 16 08:38:39 2006   anonymous

Pretty Neat stuff. $_ holds the last variable/command passed.


Mon Mar 19 00:00:25 2007   anonymous

Honestly guys, who cares! You all are looking at this as a script of sorts.
Most users will mainly use cp, cat, ls, dir, rm, mkdir,rmdir, vim, chown,
cd, and a few others, not $. If you use SuSE then yast will do everything
that is needed for the most part.



Rodney


Mon Apr 2 00:19:46 2007   k8to

I do not feel this is a very useful question to users who have grown up
on bash. $_ has never been useful to me as a shell scripter, and for interactive
use simply editing previous lines covers any number of complex bourne and
cshisms.



I have, until this question, never even heard of the $_ variable in shell
context, although in the context of this question, /etc was the obvious
one to guess (and not because of the idea of being there last).


Fri Mar 7 15:54:14 2008   Charul


I think correct answer is /tmp


Fri Mar 7 17:41:56 2008   TonyLawrence


No, it is not. Read comments above.


Wed Mar 12 13:37:37 2008 Hi Charul  anonymous


the '_' is infact an environment variable where the argument passed to
the previous executed comment is stored.So in this case it will have the
value "/etc". Hence the answer is /etc


Wed Apr 2 06:27:38 2008 tuff question  anonymous

margadarsi.com
this answer will find out only after using the above commands


Sun May 18 21:45:40 2008   Bogdan

http://nimblex.net
$_ is the variable that stores the last parameter of the last command />



Wed May 28 17:56:24 2008   anonymous


Useless knowledge for doing Linux System Administration...


Wed May 28 18:03:58 2008   TonyLawrence


Sigh.. that's not the point. Go back to the beginning and read why I did
these tests.


Tue Oct 7 21:15:24 2008   JimC


Question was what directory would you be left in.

I tested it, so he was right.


Sun Oct 26 15:37:56 2008   Robert


After you do your ls /etc

do a echo $_ and you'll see it says /tmp. $_ shows the last variable used.


Fri Dec 12 06:39:09 2008   anonymous


Depends on the shell, duh, does anyone remember csh or tcsh? How about
bourne?



Ok, in bash or korn, stores the last argument of the previous command executed,


so "/etc" is correct.


Fri Dec 12 12:10:03 2008   TonyLawrence


The question asks: "Using bash" ..


Fri Dec 26 21:25:45 2008   Adam


i think the biggest confusion with this command is the lack of understanding
/ explanation of the answer / result of the command. I'm currently studying
for my Linux + im a totally new user and i just tried the command. and found
that it is the /ect directory because of the explanation of "$_ holds the
last variable/command passed." i tried the command on a few different directories
and that statement holds true. i think if there were a little better explanation
/ more patience it would be easier to understand these questions / answers




btw thank you for the site its very interesting to read and go through
during my studies.


Fri Dec 26 21:35:58 2008   TonyLawrence


You are so right..

This one question has caused more confusion and argument than all the others.

Cleaning up these tests is on my 2009 resolution list..


Fri Jan 9 06:17:52 2009   edward




According to bash manual:

_ special parameter - Subsequently, expands to the last argument to the
previous command, after expansion. The previous command was ls /etc, thus
previous argument /etc has set _ to this value. cd $_ resulted to moving
to /etc directory.


Wed Jan 28 15:34:14 2009 it is correct  anonymous


correct answer which was specified /etc


Tue Mar 3 06:38:14 2009   anonymous


correct answer


Wed Jul 1 09:04:59 2009   Nenad


Just because this question has caused so much debate - it is a VERY good
one. If a person doesn't understand what it does and how it works, but tends
to answer without even WANTING to try to execute it or at least read through
and understand it - they should just simply fail any test they get their
hands on!



The question is nice, simple, and can confuse you if you do not know how
strings are expanded - and yet has only one answer.



Great!






Mon Sep 14 20:09:40 2009 Very Educational  anonymous


I first thought it was /tmp as well but after executing the commands it
was /etc. it also lead me to play around with many other cd and ls variables
and as impressed with what I never knew was there.


Tue Sep 15 18:32:15 2009   anonymous


I know it's significantly more work, but it would help people to learn
so much better if the answer were more than a simple indication of which
was the right choice.



It should include a brief explanation of what the example is intended to
teach.



E.g. in this case it could say something like "/etc -- In bash, $_ is the
(expanded) last argument to the previous command.".


Thu Sep 17 20:25:41 2009   TonyLawrence


I agree, and have been doing that in some places. It is a lot of work as
you note, and work takes time. We'll get there eventually.


Add your comments


Next question
Return to Question q0613

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.