rsr
rsr

Reputation: 13

How can I execute list of commands from a file in Bash

I have a file "commands.txt" with some commands in it for example:

pwd
wc -l commands.txt

And when I run the following command, its not executing the commands.

export IFS=$'\n' # I did this so that I could avoid breaking up the command line from the file
for i in `cat commands.txt`; do
    $i
done

Upvotes: 1

Views: 5261

Answers (1)

iruvar
iruvar

Reputation: 23374

Since you are setting IFS to \n, your second line wc -l commands.txt is not being word-split correctly and is being treated as a single command instead of the command wc followed by parameter commands.txt. Do not set IFS, use a while loop instead

while read -r com; 
do 
    $com; 
done < commands.txt

Upvotes: 4

Related Questions