How to Get the Variable Value Inside the Eof Tags

How can I get the variable value inside the EOF tags?

Remove the backslash before EOF:

#!/bin/bash

i=ok

# This prints "Bwah ok"
cat <<EOF
Bwah $i
EOF

# This prints "Bwah $i"
cat <<\EOF
Bwah $i
EOF

To get your last line display rightsubnet="10.109.0.20/32" (for i=1), you need something like this:

i=1
val1=beep
val2=bop

rightval="val$i"
cat <<EOF
This is a beep: ${!rightval}
EOF

That is, you compute the name of the variable you want, put that in another variable, and use the ${!var} syntax.

But for that kind of thing you should rather use an array:

i=0
vals=(beep bop)

cat <<EOF
This is a beep: ${vals[$i]}
EOF

Note however that the indexes start at 0.

Use variable with the EOF to generate a file

Heredocs can be used in a few different ways:

  1. Standard - this allows for expansion of parameters, but must have closing word hard against left edge:

cat <<EOF
...
EOF


  1. Indented - here you can use tabs to indent both text and closing word

cat <<-EOF
[tab][tab]...
[tab]EOF


  1. Quoted - this version prevents any expansions from being done, which can be useful if you are using it to create another script with variables.
    If outputting to a file, all the tabs at the start of each line will be removed

cat <<'EOF'
...
EOF

You can also combine 2 and 3.

How can I capture a result from EOF and put into a variable in bash?

You want a here document for your commands, then redirect the output to some file.

Something like this:

sqlplus -s /nolog > myfile.txt <<EOF
conn c##myuser/mypassowd
col product for a20
select product from myuser.table where id = 10;
EOF

I don't have sqlplus handy to test it myself, but the above should be "close"...

This should be able to put it into a variable:

myvar=$(sqlplus -s /nolog <<EOF
conn c##myuser/mypassowd
col product for a20
select product from myuser.table where id = 10;
EOF
)

How to process or excape variables inside of EOF to write file content?

Using only the delimiter itself, either all parameters are expanded or none. You'll have to allow expansion, but escape the dollar signs for $uri to inhibit their expansion.

if [ "$type" = "nginx" ]; then
cat > "${path}/nginx.conf" <<EOF
server {
listen $port;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files \$uri \$uri/ /index.html = 404;
}

include /etc/nginx/extra-conf.d/*.conf;
}
EOF
fi

The here document behaves like a double-quoted string:

$ foo=bar
$ echo "$foo"
bar
$ echo "\$foo"
$foo

How does cat EOF work in bash?

This is called heredoc format to provide a string into stdin. See https://en.wikipedia.org/wiki/Here_document#Unix_shells for more details.


From man bash:

Here Documents


This type of redirection instructs the shell to read input from
the current source until a line
containing only word (with no trailing
blanks) is seen.

All of the lines read up to that point are then used as the
standard input for a command.

The format of here-documents is:

          <<[-]word
here-document
delimiter

No parameter expansion, command substitution, arithmetic expansion, or
pathname expansion is performed on
word. 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.
If word is unquoted, all lines of the
here-document are subjected to parameter expansion, command
substitution, and arithmetic
expansion. In the latter case, the
character sequence \<newline> is
ignored, and \ must be used to quote the characters \, $, and `.

If the redirection operator is <<-, then all leading tab characters
are stripped from input lines and the
line containing delimiter. This
allows here-documents within shell scripts to be indented in a natural fashion.

How to define a bash variable from python EOF

I want to be able to share a variable by defining it in my CMD

That is not possible.

I want to

Write a Bash builtin or path Bash with a modification that creates a shared memory region and introduces a new special variable that uses that shared memory region to store and fetch the value of the variable.

Use the shared memory region from python to store the value of the variable.

You could also go three steps further, and just straight introduce a Bash loadable module with interface to Python with communication via a local socket or similar, similar to vim Python library.


You can:

  • Output the value and use a command substitution to capture the output of python process and assign that output to a variable.

    var=$(python -c 'print(line)')
    echo "$var"
  • Store the value to a file and then read the content of that file in Bash. Or similar.

    python -c 'open("/tmp/temporaryfile.txt").write(line)'
    var=$(cat /tmp/temporaryfile.txt)
    echo "$var"

You may want to research "processes" - what they are, what do they share and what they don't share, and what are the methods of communication between processes.

How to assign a heredoc value to a variable in Bash?

You can avoid a useless use of cat and handle mismatched quotes better with this:

$ read -r -d '' VAR <<'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF

If you don't quote the variable when you echo it, newlines are lost. Quoting it preserves them:

$ echo "$VAR"
abc'asdf"
$(dont-execute-this)
foo"bar"''

If you want to use indentation for readability in the source code, use a dash after the less-thans. The indentation must be done using only tabs (no spaces).

$ read -r -d '' VAR <<-'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
$ echo "$VAR"
abc'asdf"
$(dont-execute-this)
foo"bar"''

If, instead, you want to preserve the tabs in the contents of the resulting variable, you need to remove tab from IFS. The terminal marker for the here doc (EOF) must not be indented.

$ IFS='' read -r -d '' VAR <<'EOF'
abc'asdf"
$(dont-execute-this)
foo"bar"''
EOF
$ echo "$VAR"
abc'asdf"
$(dont-execute-this)
foo"bar"''

Tabs can be inserted at the command line by pressing Ctrl-V Tab. If you are using an editor, depending on which one, that may also work or you may have to turn off the feature that automatically converts tabs to spaces.



Related Topics



Leave a reply



Submit