Search code examples
linuxshellappendcattee

How to append some lines having variable name using shell script


I want create/append a file using bash script using tee command but lines which i want to add/append in file having variable name.when i execute that script it will showing blank in place of variable name but i want that variable name as it is there. My script is append.sh having below code

#!/bin/bash
tee config.json <<EOL
"metrics": {
                "append_dimensions": {
                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
                        "ImageId": "${aws:ImageId}",
                        "InstanceId": "${aws:InstanceId}",
                        "InstanceType": "${aws:InstanceType}"
                },
EOL

Now when i execute command "cat config.json" it showing blank in place of variable name (${aws:AutoScalingGroupName}) etc.

`"metrics": {
        "append_dimensions": {
            "AutoScalingGroupName": "",
            "ImageId": "",
            "InstanceId": "",
            "InstanceType": ""
        },`

But i want all the lines as it is as i write in the script Please tell me how i achieve this.


Solution

  • You need to quote EOL to disable expansions within the here-document it introduces. Like:

    tee config.json <<'EOL'
    "metrics": {
                    "append_dimensions": {
                            "AutoScalingGroupName": "**${aws:AutoScalingGroupName}**",
                            "ImageId": "**${aws:ImageId}**",
                            "InstanceId": "**${aws:InstanceId}**",
                            "InstanceType": "**${aws:InstanceType}**"
                    },
    EOL