Home > Net >  How to assign multiple echo outputs to a variable and store it a text file
How to assign multiple echo outputs to a variable and store it a text file

Time:07-15

I am trying to read multiple echo outputs and assign it to a variable and store it a text file one by one.

echo docker-virtual.artifactory.com/dotnetcore:latest
echo docker-virtual.artifactory.com/images:latest
echo docker-virtual.artifactory.com/nginx:latest

var="$(echo "docker-virtual.artifactory.com/dotnetcore:latest")"
echo $var > docker.txt

But here how to read multiple echo outputs and assign it to a variable and store in a text file one by one like all outputs. When I tried everytime it re-writes and store only one value, so how to achieve this.

CodePudding user response:

Isn't it simpler to assign values directly? like this

var_dotnet="docker-virtual.artifactory.com/dotnetcore:latest"
var_images="docker-virtual.artifactory.com/images:latest"
var_docker="docker-virtual.artifactory.com/nginx:latest"

If you insist on echoing, do the following:

var_dotnet=$(echo docker-virtual.artifactory.com/dotnetcore:latest)
var_images=$(echo docker-virtual.artifactory.com/images:latest)
var_docker=$(echo docker-virtual.artifactory.com/nginx:latest)

now, you can write this variables to the file:

echo "$var_dotnet" > docker.txt
echo "$var_images" >> docker.txt
echo "$var_docker" >> docker.txt

Please note ">>" - it appends to the text file, while ">" rewrites it.

  • Related