help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Here document expand partial variable


From: Chenyang Yan
Subject: Here document expand partial variable
Date: Tue, 2 Nov 2021 22:09:38 +0800

According to Bash Manual, "If any characters in word are quoted, the
delimiter is the result of quote removal on word, and the lines in the
here-document are not expanded."
I need expand partial variable in ssh command with here document.

```
[root@control foo]# cat test.sh
#!/bin/bash
service_name="foo.service"

# ssh 127.0.0.1 <<"EOF"
# do somethings ...
# EOF

#### In order to see what's the generated ssh command, redirect
content to temp script
cat >/tmp/test.sh<<"EOF"
_function_update_config()
{
    local service_filepath="$1"
    echo "do something ... with $service_filepath"
}

service_filepath=$(systemctl show -p FragmentPath "$service_name" |
cut -d"=" -f 2-)  # expected expand variable service_name
EOF
[root@control foo]# bash test.sh
[root@control foo]# cat /tmp/test.sh
_function_update_config()
{
    local service_filepath="$1"
    echo "do something ... with $service_filepath"
}

service_filepath=$(systemctl show -p FragmentPath "$service_name" |
cut -d"=" -f 2-)  # expected expand variable service_name
```

Below following do work, but escaped using a backslash is ugly if
dealing with complicated function logical and variables:

```
[root@control foo]# cat test.sh
#!/bin/bash
service_name="foo.service"

# ssh 127.0.0.1 <<"EOF"
# do somethings ...
# EOF

cat >/tmp/test.sh<<EOF
_function_update_config()
{
    local service_filepath="\$1"
    echo "do something ... with \$service_filepath"
}

service_filepath=\$(systemctl show -p FragmentPath "$service_name" |
cut -d"=" -f 2-)  # expected expand variable service_name
EOF
[root@control foo]# bash test.sh
[root@control foo]# cat /tmp/test.sh
_function_update_config()
{
    local service_filepath="$1"
    echo "do something ... with $service_filepath"
}

service_filepath=$(systemctl show -p FragmentPath "foo.service" | cut
-d"=" -f 2-)  # expected expand variable service_name
```

So, have any method to expand partial variable in here document?
Thanks everyone.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]