Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I' ve got this situation:

./
./myscript.sh
./arguments.txt
./test.sh

Inside myscript.sh, i have to run the file test.sh, passing to it the arguments contained inside arguments.txt.

myscript.sh is:

arguments=$(cat arguments.txt)
source test.sh $arguments

This works well if if arguments.txt contains max one argument:

firstargument 

The substitution is:

++ source test.sh 'firstargument'

But, and here is the real problem, with two arguments it does this:

++ source test.sh 'firstargument secondargument'

Also, i don't know in advice the number of arguments inside arguments.txt. There can be zero, one or more.

Thanks a lot for help in advice!

share|improve this question
    
What you are describing is not the default behavior of bash. Are you truly using bash, or some other shell (such as zsh, which will do this)? –  Patrick Jul 21 at 18:04
    
@Patrick Hi, it is real bash. Btw i've already got the answer thanks anyway! –  Federico Ponzi Jul 21 at 18:17
    
Are you actually writing source test.sh "$arguments" with quotes? That would be one explanation for your description –  glenn jackman Jul 21 at 19:55
    
I tried both with and without double quotes. With the bash's substitution i always get the singlequote. So source test.sh "$arguments" and source test.sh $arguments both result in source test.sh 'firstargument secondargument'. –  Federico Ponzi Jul 22 at 7:38

2 Answers 2

up vote 2 down vote accepted

Assuming each line of arguments.txt represents a separate argument, with bash 4 you can read arguments.txt into an array using mapfile (each line from the file goes in as an array element, in sequence) and then pass the array to the command

mapfile -t <arguments.txt
source test.sh "${MAPFILE[@]}"

The advantage is that splitting on spaces embedded inside lines is avoided

With lower versions of bash

IFS=$'\n' read -ra arr -d '' <arguments.txt
source test.sh "${arr[@]}"
share|improve this answer
    
Inside the file arguments.txt the arguments are separeted by space. Btw i tried something very similar using array and a for but didn't worked and don't know why. Well, thanks a lot! –  Federico Ponzi Jul 21 at 18:16

You can do this with awk. For example:

arguments=`awk '{a = $1 " " a} END {print a}' arguments.txt`

Edit after reading your comment:

arguments=`awk '{i = 0; while(i<=NF){i++; a = a " "$i}} END {print a}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.