I am reading from an input file that contains few hexadecimal numbers from a bash script. Then, I want to pass those strings as command line argument of an executable file.

Say, here is my test.txt:


I am reading it through the script:

while read line || [ "$line" ]; do
done < test.txt

for n in `seq 0 $max`
./main -o $s

As you can see, the numbers are passed as command line option for ./main.

Now, the problem is, I need to keep $s within double quotes. I tried this:


but it did not work.

Any idea on how to make it work?

  • 1
    Are you sure you don't just need to double quote $s as you should always do with variables?
    – jesse_b
    Mar 30, 2018 at 17:19
  • Just use "$s"? Or du you actually need the double quotes to be part of the argument?
    – Kusalananda
    Mar 30, 2018 at 17:20
  • ...and s="${arr[$n]}". Or omit the $s entirely and do ./main -o "${arr[$n]}"
    – phemmer
    Mar 30, 2018 at 17:21
  • Adding double quotes around $s did not work.
    – hola
    Mar 30, 2018 at 17:25
  • "Does not work" is not a valid bug report.
    – Kusalananda
    Mar 30, 2018 at 17:26

2 Answers 2


From comments, it sounds as if your test.txt file comes from a Windows system and is a DOS text file. You should convert it to a Unix text file with dos2unix. Alternatively, modify the below to use tr -d '\r' <test.txt | head -n 3 in place of just head -n 3 test.txt.

You have a file with lines of text, and you need to invoke the script/program main once each for the three first of these lines:

head -n 3 test.txt | xargs -n 1 main -o

If you really need the double quotes to be part of the argument (this is unusual):

head -n 3 test.txt | xargs -I XX main -o '"XX"'

To invoke main once for each line in the file, use

xargs -n 1 main -o <test.txt

Using GNU sed's evaluate command:

sed 's#^#./main -o &#e' test.txt

And if the formatting is wrong, just precede that with some code. For example, if the format for the first line should be "./main -o 27 8d ca 53 f9 60 14 be", this would work:

sed 's/../& /g;s#^#./main -o &#e' test.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .