I have a file that contains data that gets updated over time (mydata). The file looks like this:1 2 3With this file, I want to do this (basically handle each number as a separate parameter):myscript.sh 1 2 3Since the data is not static but instead updated over time, I want to run this command:myscript.sh $(cat mydata)But instead I see ./myscript.sh: line 1: 1: command not foundWhat should I do?
How to pass the content of a file as multiple arguments in bash
bash;shell script;parameter
$(cat mydata) evaluates to a string which contains the content of the file mydata minus any trailing newline. What you want is the list of whitespace-separated words in the file. So, for once, use the command substitution outside of double quotes:myscript.sh $(cat mydata)For extra robustness, turn off globbing, so that if a column in mydata contains one of the characters \[*? it isn't interpreted as a file name pattern. You may also want to set IFS to contain the word separator characters, but the default (ASCII whitespace) should be exactly what you need.(set -f; myscript.sh $(cat mydata))Alternatively, you could use the read builtin. This reads the first line and ignores the rest (which you could do above by replacing cat by head -n 1).read -r one two three rest <mydatamyscript.sh $one $two $three