Home > OS >  linux command line: How can simply feed arbitrary strings to pipe?
linux command line: How can simply feed arbitrary strings to pipe?

Time:02-01

I'm looking for a really simply trick:

Suppose in the file error.txt, there is a really long string:

{"errorMessage": "....", "key1": "some message...", "key2": "message 2 ...", ....}

Since it is not formatted to be reader friendly, if I want to view it in the terminal in its well-formatted form, I can do: cat error.txt | jq

This will print the string in terminal in reader friendly form.

However, what if I just have that long string from copy and pasting, and I do not want to go through the trouble of pasting it in a txt file, I want to format and view it right away. Something like:

cat {"errorMessage": "....", "key1": "some message...", "key2": "message 2 ..."} | jq

where the string is not in a file, but directly pasted into the command line at real time?

How can feed arbitrarily typed strings into the pipe?

CodePudding user response:

The answer is the <<<${my_string} syntax

jq <<<'{"errorMessage": "....", "key1": "some message...", "key2": "message 2 ..."}'

It can be used to send in whatever as the stdin for a command, no superfluous echo:ing needed. I use it all the time in scripts to parse the content of variables.

CodePudding user response:

The simple and obvious solution is that you can paste information on standard input one way or another if it can be represented as a string.

echo "your data here" | cmd
cmd <<<"your data here" # bash only
cmd <<\____EOF  # this is called a "here document"
your data here
____EOF

None of this is specific to pipes.

The proper solution for completely arbitrary data might look slightly different, as there is no way to represent random bytes as strings. If the character encoding and your locale permits it, nearly every byte can be embedded in a string (ISO-8859-1 has this property; UTF-8 does not) but null bytes cannot be represented as themselves in the shell.

Bash offers a way to specify almost arbitrary strings by using an extension called "C strings" (where like in the C language you can specify arbitrary bytes with a backslash followed by octal numbers, as well as a range of predefined symbols like \t for tab, \n for newline, \a for the bell character, etc) but this too is constrained to non-null bytes (where any nulls will be silently dropped!):

echo $'\000\000\000' | wc
       1       0       1

(notice how the output shows that the null bytes are being truncated) or perhaps you can perform an encoding yourself:

echo ___ | tr _ '\000' | wc
echo AAAACg== | base64 -d | wc
printf '\000\000\000\n' | wc

Notice also how printf is much more versatile than echo, as is very often the case.

Perhaps I should also mention iconv for the fairly common use case of representing strings which are not valid UTF-8.

echo möjibäkë | iconv -f utf-8 -t iso8859-1
  •  Tags:  
  • Related