How to pass `$myVariable` in below `nohup` statement?

```bash
myVariable=`command_0`
nohup bash <<'EOF'
command_1 $myVariable
EOF
command_2 $myVariable
```
Feb 8, 1:05am

In your current script, the nohup block uses a here-document (<<'EOF') with single quotes, which prevents variable expansion inside the block. As a result, $myVariable is not expanded, and command_1 receives the literal string $myVariable instead of its value.

How to Fix It

Option 1: Remove the Single Quotes

If you want the variable to be expanded, remove the single quotes around EOF:

bash
myVariable=`command_0` nohup bash <<EOF command_1 $myVariable EOF command_2 $myVariable

This way, $myVariable is expanded before the nohup block is executed.


Option 2: Escape the Variable Inside the Here-Document

If you need to keep the single quotes (e.g., to prevent other expansions), you can escape the variable and pass it via env:

bash
myVariable=`command_0` nohup env myVariable="$myVariable" bash <<'EOF' command_1 "$myVariable" EOF command_2 "$myVariable"

This explicitly passes the value of myVariable to the subshell.


Option 3: Use a Function

If you want to keep the here-document as-is and still pass the variable, you can define a function:

bash
myVariable=`command_0` nohup bash -c ' myVariable="$1" shift command_1 "$myVariable" ' _ "$myVariable" command_2 "$myVariable"

Key Takeaway

  • Use Option 1 if you want the variable expanded in the here-document.
  • Use Option 2 if you need to keep the here-document quoted but still pass the variable.
  • Use Option 3 for more complex scenarios.
Feb 8, 1:05am

This Chat is read-only. Login to resume chatting.

myVariable=`command_0` nohup bash <<EOF command_1 $myVariable EOF command_2 $myVariable
myVariable=`command_0` nohup env myVariable="$myVariable" bash <<'EOF' command_1 "$myVariable" EOF command_2 "$myVariable"
myVariable=`command_0` nohup bash -c ' myVariable="$1" shift command_1 "$myVariable" ' _ "$myVariable" command_2 "$myVariable"