Now instead !! is not rightly evaluated.
Why?
For in script you have the history expand by default disabled.
If you want it to be like in command line, then you need to enable the >history expand
--- bash script ---
#!/bin/bash
set -o history -o histexpand
ls /tmp > /dev/null
echo "CMD='"!:0"'"
--- eof ---
but it has it's bad sides as you see, you may use trap for better
debugging, but will not really works as you seems to want and I can't
see an user case where you would want to do your example.
OP has not stated what his actual goal is, so we don't know anything
about what the actual use case is.
[...]$ rm typescript
Il 30/06/22 14:04, Kenny McCormack ha scritto:
OP has not stated what his actual goal is, so we don't know anything
about what the actual use case is.
$ tree dir | cat >> commands-output
After a few days (in which I will have forgotten what I did exactly, in[snip]
which I forgot the command I have in given...)
$ cat commands-output
dir
├── a
├── b
└── c
The output refers to what?
Or rather, Output refers to which command?
So at the first line I would have liked to have the reference command,
and then everything else.
It can be done?
-----BEGIN PGP MESSAGE-----[snip]
-----END PGP MESSAGE-----And, why the encrypted message?
2) OP might be looking for $_, which generally holds the name of the
last command executed (at least in bash;
don't know about any other
shells). This works in scripts. I sometimes use $_ to tell me
whether or not a loop found a match (i.e., succeeded) or just ended
because the loop ended. For example:
for i in {list of files};do
[ $i matches or whatever ] && break
done
[ $_ = break ] && echo "A match was found!" || echo "Loop exited without finding a match"
Il 30/06/22 14:04, Kenny McCormack ha scritto:
OP has not stated what his actual goal is, so we don't know anything
about what the actual use case is.
$ tree dir | cat >> commands-output
After a few days (in which I will have forgotten what I did exactly, in
which I forgot the command I have in given...)
I want to send the output only to the "cat -n" command and not to the typescript file.
Il 30/06/22 14:04, Kenny McCormack ha scritto:
OP has not stated what his actual goal is, so we don't know anything
about what the actual use case is.
$ tree dir | cat >> commands-output
After a few days (in which I will have forgotten what I did exactly, in
which I forgot the command I have in given...)
$ cat commands-output
dir
├── a
├── b
└── c
The output refers to what?
Or rather, Output refers to which command?
So at the first line I would have liked to have the reference command,
and then everything else.
Example
$ cat commands-output
REFERENCE COMMAND: $ tree dir
dir
├── a
├── b
└── c
It can be done?
On 30.06.2022 14:04, Kenny McCormack wrote:
2) OP might be looking for $_, which generally holds the name of the
last command executed (at least in bash;
Does it? - In ksh $_ keeps the last word of the previous command line,
so 'break' or 'ls' will return the command name (incidentally), and
'ls a b c' will return 'c'. - A quick bash test seems to indicate that
it behaves like ksh in that respect.
In article <t9kk0u$1v7i7$[email protected]>,
Janis Papanagnou <[email protected]> wrote:
On 30.06.2022 14:04, Kenny McCormack wrote:
2) OP might be looking for $_, which generally holds the name of the >>> last command executed (at least in bash;
Does it? - In ksh $_ keeps the last word of the previous command line,
so 'break' or 'ls' will return the command name (incidentally), and
'ls a b c' will return 'c'. - A quick bash test seems to indicate that
it behaves like ksh in that respect.
What can I say? It works for me.
Two comments:
1) The test should actually be:
[ X$_ = Xbreak ] && echo "It broke!"
since otherwise you have a problem if $_ is empty (which I think it
will be if you exit the loop "normally").
2) My actual use case was a little more complicated than the "distilled
down for posting to Usenet" version, and I was having problems
getting it to work reliably doing it your way.
In general, I'm not
real comfortable with relying on "default" exit statuses (i.e.,
those not explicitly set in code).
(set -x; tree a) >commands-output 2>&1
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (3 / 13) |
| Uptime: | 142:58:32 |
| Calls: | 12,089 |
| Calls today: | 2 |
| Files: | 14,998 |
| Messages: | 6,517,455 |