bash - back ticks messing with output of command -


for example:

ipconfig > temp cat temp 

yields correct results (all output ipconfig).

however:

ipconfig > temp echo `cat temp` 

yields wrong results:

connection-specific dns suffix . :: media disconnected2a612}:%62 

that's it, nothing more being printed, wrong becuase if open temp in editor can see correct output.

to make work, change echo `cat temp` echo "`cat temp`".

there several reasons why experiencing issue:

unquoted command substitution

echo `cat temp` form of unquoted command substitution, result in word splitting (and globbing).

whitespace, includes newline character \n, considered delimiter bash, treat bit of text separated whitespace element in array, , pass each element own argument whatever command uses substitution. echo output each argument receives separated spaces:

$ echo `echo line 1; echo line 2; echo line 3` line 1 line 2 line 3 

however, when quote substitution, word splitting not applied, \n treated string literal:

$ echo "`echo line 1; echo line 2; echo line 3`" line 1 line 2 line 3 

windows line endings

windows, unlike *nix os's, doesn't use newline character \n line break. instead, uses combination of carriage return character \r , newline character \n, or in other words, \r\n.

in posix environment, \r in terminal, on typewriter, returns current position beginning of line.

therefore, usually won't see of issue reading windows-formatted file on linux, because while has \r characters, still includes \n. 2 being used in tandem has same effect: return front of line , move next line. redundant, still gets same place.

so why happening, then?

keep in mind said usually doesn't cause issue have \r characters when reading file (or stream, or else) in *nix. here have example of situation isn't case.

here's step-by-step of what's happening when run ipconfig > temp; echo `cat temp`:

  1. ipconfig.exe runs , output redirected file temp.
  2. cat temp runs , output parsed bash.
  3. the command substitution unquoted, bash splits results whitepsace.
  4. the split result passed echo.
  5. echo prints arguments separated spaces.
  6. eventually, echo prints last piece of text on first line outputted ipconfig.exe. however, line contains \r @ end, ipconfig.exe windows command , uses windows line endings, after bit of text printed, position in terminal returns beginning of line.
  7. echo continues print arguments, each time prints \r, subsequent output overwrites text printed beginning of line.

and why output do. stated, got

connection-specific dns suffix . : media disconnected2a612}:%62 

therefore, i'm guessing last line ipconfig.exe outputted was

connection-specific dns suffix . : media disconnected 

and longest line of text printed ended in 612}:%62 (probably ipv6 address), , second longest ended in 2a.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -