Output piped command to variable Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Output piped command to variable without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’am trying to write a small script to create an JSON File. I have to store SSL Certificates inside this json, so i have to convert the CRT/KEY file to the required format.

Is there a Solution to store the result of:

cat $domain.key | sed s/$/\n/ | tr -d 'n'

inside an Variable (bash.script)

input_key=`cat $domain.key | sed s/$/\n/ | tr -d 'n'`

Does not run the “sed” after the cat (-:

Answer

This’ll do it:

input_key=$(cat ${domain}.key | sed 's/$/\n/' | tr -d 'n')

The sed is running in your example, but I believe something in the regex bits are getting interpreted by the shell. You simply need to wrap the regex in single quotes to avoid any shell interpretation. The following also works:

input_key=`cat ${domain}.key | sed 's/$/\n/' | tr -d 'n'`
We are here to answer your question about Output piped command to variable - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji