0

For Simplicity, consider the following:

x=$(java --version)
echo "x: $x"

It works how expected showing:

x: openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment Temurin-11.0.13+8 (build 11.0.13+8)
OpenJDK Client VM Temurin-11.0.13+8 (build 11.0.13+8, mixed mode)

What I need is the following:

x=$(java --version)
echo "x: $x"
echo "The '???' command was executed"

Therefore the output should be

x: openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment Temurin-11.0.13+8 (build 11.0.13+8)
OpenJDK Client VM Temurin-11.0.13+8 (build 11.0.13+8, mixed mode)
The 'java -version' command was executed

With what can be replaced ????

I already do a research and I found about set +x/-x but does not apply here in a 100%

Manuel Jordan
  • 1,728
  • 2
  • 16
  • 40

3 Answers3

2

Not sure, why - in a script - you wouldn't know which command had just been executed. But, for the fun of it:

exec 2>tmpfile; set -x
x=$(java --version)
set +x; exec 2>&1
awk  'sub(/^\+\+\+/, "") {print "The \"" $0 "\" command was executed."} ' tmpfile
The " java --version" command was executed.

The awk command removes the $PS4 characters (which, in this case, are RE meta chars as well) introduced by the -x (xtrace) option, then prints the required line. The tmpfile should be removed afterwards.

RudiC
  • 8,969
1

In interactive shell when history expansion is enabled you can do:

$ java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)
$ command="!!"
$ echo "$command"
java -version
  • I am creating the scripts for automation purposes for some most complex commands working with parameters (based in user input) and including more pipes - so all works through Menu options. – Manuel Jordan Feb 13 '22 at 16:01
  • Consider to indicate how enable/disable history expansion - seems it is disabled in my side – Manuel Jordan Feb 13 '22 at 16:03
1

As @Arkadiusz Drabczyk says !! is the last command executed.

This trick have other interesting use ways, as man bash says :

!-n Refer to the current command line minus n.

so : !-2 is what you want.

The down side is that this work only in interactive shell.

To enable that behavior in a script you need to specify it to bash with the following option :

#!/bin/bash
set -H
set -o history
x=$(gcc -v 2>&1) # Sorry i dont like Java
echo "x is $x"
echo "Last cmd is " !-2 2>&1 >/dev/null

Or you can just enable history inside you script and parse its output:

#!/bin/bash
set -o history
x=$(gcc -v 2>&1)
echo "x is $x"
last_cmd=$(history | sed -n '1s/^ * *//p')
echo "The cmd executed is $last_cmd"

The part ... | sed ... cuts off the format used by the history command. In my system, history, print out :

  2142  ./test2.sh 
  2143  cat test2.sh 
  2144  history 

On other systems, the date and time may also be printed.


See these for interesting info on how enable history inside a script and history expansion inside a script