From: George on
Dear All,

I am a bit confused with the way bash history works. Say, for example,
that I am running the following command:

ls -l test1/

Now, if I do not remember the name of the previous directory I used but
now I want to run an ls on the directory test2, I try running:

!!:s/!!:1:p/test2 (to get the command echoed without executing)

but I am getting the message that substitution failed.

I know that I can use the up arrow to go back and simply substitute the
name; what I am asking is why the history command does not work. I went
through online tutorials and cheat sheets with event and word
designators but can not see what I am doing wrong.

Regards,
George
From: Janis Papanagnou on
George wrote:
> Dear All,
>
> I am a bit confused with the way bash history works. Say, for example,
> that I am running the following command:
>
> ls -l test1/
>
> Now, if I do not remember the name of the previous directory I used but
> now I want to run an ls on the directory test2, I try running:
>
> !!:s/!!:1:p/test2 (to get the command echoed without executing)

(This is really aweful syntax, if you ask me.)

If you don't mind another approach, use the history command fc;

$ ls -l test1
$ fc -s 1=2 ls

The second command will replace 1 by 2 in the _previous ls command_,
or use just

$ ls -l test1
$ fc -s 1=2

to replace 1 by 2 in the _previous command_.

Janis

>
> but I am getting the message that substitution failed.
>
> I know that I can use the up arrow to go back and simply substitute the
> name; what I am asking is why the history command does not work. I went
> through online tutorials and cheat sheets with event and word
> designators but can not see what I am doing wrong.
>
> Regards,
> George
From: George on
On 01/06/2010 17:12, Janis Papanagnou wrote:
> George wrote:
>> Dear All,
>>
>> I am a bit confused with the way bash history works. Say, for example,
>> that I am running the following command:
>>
>> ls -l test1/
>>
>> Now, if I do not remember the name of the previous directory I used but
>> now I want to run an ls on the directory test2, I try running:
>>
>> !!:s/!!:1:p/test2 (to get the command echoed without executing)
>
> (This is really aweful syntax, if you ask me.)
>
> If you don't mind another approach, use the history command fc;
>
> $ ls -l test1
> $ fc -s 1=2 ls
>
> The second command will replace 1 by 2 in the _previous ls command_,
> or use just
>
> $ ls -l test1
> $ fc -s 1=2
>
> to replace 1 by 2 in the _previous command_.
>
> Janis
>

Thanks Janis for the prompt reply. I agree that the syntax is ugly, but
I am just curious as to why it would not work. Also, the test1 and test2
directories are just examples - it could be bin and var or anything
else. The idea is that you know you have run an ls -l command on a
directory, you want to replace that directory name with a new one.

I thought it will do it if I use the s designator for substitute and
then !!:1 would be the first argument of the previous command.

I just can't understand what is wrong there.

>>
>> but I am getting the message that substitution failed.
>>
>> I know that I can use the up arrow to go back and simply substitute the
>> name; what I am asking is why the history command does not work. I went
>> through online tutorials and cheat sheets with event and word
>> designators but can not see what I am doing wrong.
>>
>> Regards,
>> George

From: Janis Papanagnou on
George wrote:
> On 01/06/2010 17:12, Janis Papanagnou wrote:
>> George wrote:
>>> Dear All,
>>>
>>> I am a bit confused with the way bash history works. Say, for example,
>>> that I am running the following command:
>>>
>>> ls -l test1/
>>>
>>> Now, if I do not remember the name of the previous directory I used but
>>> now I want to run an ls on the directory test2, I try running:
>>>
>>> !!:s/!!:1:p/test2 (to get the command echoed without executing)
>>
>> (This is really aweful syntax, if you ask me.)
>>
>> If you don't mind another approach, use the history command fc;
>>
>> $ ls -l test1
>> $ fc -s 1=2 ls
>>
>> The second command will replace 1 by 2 in the _previous ls command_,
>> or use just
>>
>> $ ls -l test1
>> $ fc -s 1=2
>>
>> to replace 1 by 2 in the _previous command_.
>>
>> Janis
>>
>
> Thanks Janis for the prompt reply. I agree that the syntax is ugly, but
> I am just curious as to why it would not work. Also, the test1 and test2
> directories are just examples - it could be bin and var or anything
> else. The idea is that you know you have run an ls -l command on a
> directory, you want to replace that directory name with a new one.

You should be aware that you can do more complex substitutions with the fc
command than just replacing 1 by 2. And also consider that, while your bash
substitution is not only ugly but also non-standard, fc is a Unix standard
(see http://www.opengroup.org/onlinepubs/007908775/xcu/fc.html).

If you want someone to explain problems with bash specifics I really can't
help you. Sorry.

Janis

>
> I thought it will do it if I use the s designator for substitute and
> then !!:1 would be the first argument of the previous command.
>
> I just can't understand what is wrong there.
>
>>>
>>> but I am getting the message that substitution failed.
>>>
>>> I know that I can use the up arrow to go back and simply substitute the
>>> name; what I am asking is why the history command does not work. I went
>>> through online tutorials and cheat sheets with event and word
>>> designators but can not see what I am doing wrong.
>>>
>>> Regards,
>>> George
>
From: Dave Gibson on
George <me(a)me.com> wrote:
> Dear All,
>
> I am a bit confused with the way bash history works. Say, for example,
> that I am running the following command:
>
> ls -l test1/
>
> Now, if I do not remember the name of the previous directory I used but
> now I want to run an ls on the directory test2, I try running:
>
> !!:s/!!:1:p/test2 (to get the command echoed without executing)
>
> but I am getting the message that substitution failed.

History expansions aren't nestable.

Use the range word designator with a missing end-range ":0-" to select
all but the last word:

!!:0- test2