0

I'm storing commands in a file to be read and run line by line by a POSIX shell program. It looks something like this:

curl -fLo $HOME/.antigen.zsh git.io/antigen
curl -fLo $HOME/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
vim +"so $HOME/.vimrc" +PlugInstall +qa!

I'm also using this small function body to go through it and run every line:

while read -r line; do
    $line
done < file

Simple stuff. And it works! However, I am having trouble expanding $HOME to my home directory (and ~ for that matter). I've tried using an exec subshell and removing the -r from the read loop but the curl statements create a '$HOME' directory, which is not what I want to do, I want the commands to target my /home/.\+/ directory.

Since this is a strange question and you'll probably be wondering at this point (I certainly would), this is not an XY problem. I have spent a considerable time designing this piece of software and am certain that I need to store these commands in a file for my program to work and I won't consider doing otherwise unless this is proven absolutely impossible. Also, I'm not expanding $HOME myself because I want the commands to work in other users' computers.

Any ideas? Thanks in advance!

Groctel
  • 130
  • 11
  • `sh -c "$line"`? Or `eval $line`? Usually `eval` is regarded as dangerous; I'm not sure that `sh -c` is much different, though. Come to think of it, why not simply execute the file storing the commands? `sh "$file"`? You can use `sh -e "$file"` to stop on an unchecked error, and add `-x` to see what is being executed. – Jonathan Leffler Sep 15 '20 at 23:28
  • Sorry for the late reply and thank you very much @JonathanLeffler. `sh -c "$line"` works flawlessly and solves the problem. Add it as a definitive answer please! – Groctel Sep 16 '20 at 07:48

1 Answers1

1

Transferring comments into an answer.

Can you use:

sh -c "$line"

Or:

eval $line

Usually eval is regarded as dangerous, but I'm not sure that sh -c is much different. Come to think of it, why not simply execute the file storing the commands?

sh "$file"

You can use sh -e "$file" to stop on an unchecked error, and add -x to see what is being executed.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278