Sign up ×
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:


Inside, i have to run the file, passing to it the arguments contained inside arguments.txt. is:

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

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


The substitution is:

++ source 'firstargument'

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

++ source '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 advance!

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 '14 at 18:04
@Patrick Hi, it is real bash. Btw i've already got the answer thanks anyway! –  Federico Ponzi Jul 21 '14 at 18:17
Are you actually writing source "$arguments" with quotes? That would be one explanation for your description –  glenn jackman Jul 21 '14 at 19:55
I tried both with and without double quotes. With the bash's substitution i always get the singlequote. So source "$arguments" and source $arguments both result in source 'firstargument secondargument'. –  Federico Ponzi Jul 22 '14 at 7:38

3 Answers 3

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 "${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 "${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 '14 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

I would suggest using a function with a while/do loop to iterate through the argument file.

Just create a file containing the the function and then call the the file within the function to iterate through the arguments contained in the arguments.txt file.

# Calling script

function_name ()
    while read line;
        . ~/path_to/ $line
         do_something_commands # print to screen or file or do nothing
      done < ~/path_to_/argument_file.txt

function_name # Call the function
  do_something_commands # print to screen or file or do nothing
share|improve this answer

Your Answer


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.