Article View: comp.unix.shell
Article #4011Re: Get previous command history number from a script
From: Scott Lurndal
Date: Wed, 03 Jun 2009 14:57
Date: Wed, 03 Jun 2009 14:57
23 lines
1803 bytes
1803 bytes
Ben <bgold12@gmail.com> writes: >I've already figured out a way to do this but I suspect there are >simpler/faster/more elegant solutions that exist. > >The objective is to get the command history number of the command that >was just executed, but do it from a script (which seems to prevent >access to the $HISTCMD variable, which would've been nice; any >thoughts on this?). Here's my solution: > >history 2| sed -n 1p| sed -e "s/^\s*\([0-9]\+\).*/\1/"; > >Notice the (command complexity) / (problem complexity) ratio. Can >anyone improve it? Note that if you have multiple shells open, they'll be sharing the same history file. Depending on why you want the last command executed, this could be a problem. You can arrange in your .profile/.kshrc/.bashrc file for each shell to have a different history file by setting the HISTFILE environment variable to a session-unique name. scott
Technical Details
Message-ID:
<4a268f67$0$25234$b96e1c2@news.usenetserver.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!209.197.12.218.MISMATCH!post02.iad!not-for-mailReferences:
<aa76cb4f-d625-49fb-9d0b-5480001cfd1e@q37g2000vbi.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 4d82dd0a1,5ff149a7a3349cd6 X-Google-Attributes: gid4d82dd0a1,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!209.197.12.218.MISMATCH!post02.iad!not-for-mail X-newsreader: xrn 9.03-beta-14-64bit Sender: scott@althor.sl.home (Scott Lurndal) From: scott@slp53.sl.home (Scott Lurndal) Reply-To: slp53@pacbell.net Subject: Re: Get previous command history number from a script Newsgroups: comp.unix.shell,comp.unix.programmer,gnu.bash References: <aa76cb4f-d625-49fb-9d0b-5480001cfd1e@q37g2000vbi.googlegroups.com> Date: 03 Jun 2009 14:57:43 GMT Lines: 23 Message-ID: <4a268f67$0$25234$b96e1c2@news.usenetserver.com> X-Complaints-To: abuse@usenetserver.com