On powershell for linux the print out of a maven command has additional characters

54 Views Asked by At

I am using powershell 7.4.1 on Linux and Windows.

And I used following commands to get the current target version of my maven project.

$version = mvn help:evaluate -Dexpression='project.version' -q -DforceStdout

or

$version = mvn exec:exec -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive -q

if I print the $version it looks like that they are working on both platform and are returning 2.68.0-SNAPSHOT

However I experienced some problems on my Linux system. So after digging I found the cause in the commands above. Because if I print the output as hex values to what that string contains.

I get on Linux

32 2E 36 38 2E 30 2D 53 4E 41 50 53 48 4F 54 20 1B 5B 30 6D

on Windows

32 2E 36 38 2E 30 2D 53 4E 41 50 53 48 4F 54

So does anybody know why there are 20 1B 5B 30 6D this additional characters on my Linux system. And how can I get rid of them (except replacing them afterwards)?

1

There are 1 best solutions below

3
mklement0 On BEST ANSWER

The string you're receiving contains ANSI/VT escape sequences, which are typically used for producing colored terminal output.

If the intent is to receive data only, as in your case, these escape sequences are undesired.

  • If available, it is best to ask an external program such as mvn not to produce colored output to begin with.

    • According to the Maven CLI Options Reference, you can use -B (or --batch-mode) to disable coloring;[1] e.g.:

      $version = 
        mvn -B help:evaluate -Dexpression='project.version' -q -DforceStdout
      
      • Caveat: As a general syntax requirement, due to an unfortunate, long-standing PowerShell bug, arguments that contain . in the unquoted part following the parameter name must have their initial - escaped as `-.
        For instance, -Dexec.executable='echo' must be passed as
        `-Dexec.executable='echo' - see this answer for background information.
  • If not, you can remove these escape sequences with the help of the [System.Management.Automation.Internal.StringDecorated], available in PowerShell (Core) 7+ only (v7.2+):

    • A simple example: The sample string contains escape sequences that render the word "green" in green; the .ToString('PlainText') method call strips (removes) these escape sequences.

      # Sample input string that contains ANSI/VT escape sequences.
      $stringWithEscapeSequences = "It ain't easy being $([char]27)[32mgreen$([char]27)[m."
      
      # -> "It ain't easy being green."
      [System.Management.Automation.Internal.StringDecorated]::new(
        $stringWithEscapeSequences
      ).ToString('PlainText')
      

[1] The documentation link is to the current stable version of mvn (via a /current/ segment in the URL, which turns into the concrete version number in the browser), which is v3.9.6 as of this writing. The same page is available down to version 3.1.0 and shows the -B (--batch-mode) option too; whether even earlier versions also support it, I don't know - mvn --help may reveal that.