0

On my Linux machine - I do the following:

Create a script named script.sh

 #!/bin/bash    
 variable=`df -hP`    
 echo < my_file.txt

Then I create an external file called my_file.txt

The disk usage is: $variable

When I execute the script.sh I get nothing printed.

Expected output is:

The disk usage is: Filesystem      Size  Used Avail Use% Mounted on
udev            3.2G     0  3.2G   0% /dev
tmpfs           651M  9.2M  642M   2% /run
/dev/sda1       218G  9.5G  197G   5% /
tmpfs           3.2G   62M  3.2G   2% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           3.2G     0  3.2G   0% /sys/fs/cgroup
/dev/sda4       266G  1.1G  252G   1% /home
/dev/sda2       923M  137M  723M  16% /boot
tc              308G  158G  151G  52% /media/sf_tc
tmpfs           651M   52K  651M   1% /run/user/1000

1 Answers1

2

You could export your variable and use envsubst:

#!/bin/bash
export variable=$(df -hP)
envsubst '$variable' < my_file.txt

If you use the form envsubst < my_file.txt, then all exported variables are substituted. It's safer to explicitly list all your variables you want to substitute, like

envsubst '$var1 $var2 $var3' < file

Related:

Freddy
  • 25,565