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.

Given:

$ cat lines.txt
a/b
'c/d e/f'

$ cat 1.sh
#!/bin/sh
./2.sh `cat lines.txt`

$ cat 2.sh
#!/bin/sh
echo p1=$1
echo p2=$2
echo p3=$3

$ ./1.sh
p1=a/b
p2='c/d
p3=e/f'

How do I change lines.txt or 1.sh, so that 2.sh receives only two arguments:

  • a/b
  • c/d e/f

?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Set the IFS (internal field separator) to a newline:

$ cat 1.sh 
#!/bin/sh
OLD_IFS=$IFS
IFS="
"
./2.sh `cat lines.txt`
IFS=$OLD_IFS

$ ./1.sh
p1=a/b
p2='c/d e/f'
p3=
share|improve this answer

If you aren't locked into the idea of using cat to send as many args as the file is long, you may find this method quite flexible. You can pipe file directly to 2.sh

# echo -e "a/b\n'c/d e/f'" >lines.txt

fn2.sh() { set -f              # disable pathname expansion 
   while IFS= read -r line; do # -r prevents expansion of such things as \n
       printf "%s\n" "$line"   # printf is more robust than echo (when dealing with unknown data)
   done 
}
< lines.txt fn2.sh             # using redirection 
share|improve this answer
    
The real file contains about 60 lines. So, I cannot just list them on the command line. –  mark Aug 25 '11 at 9:50

If your input file contains the line 'c/d e/f' and you want to process the item c/d e/f (without the quotes), then you may be looking for xargs. Its input format is a space-separated list of strings, with the characters ' and " delimiting literal strings. Also, \ makes the next character be interpreted literally, but only outside a string. For example, here's an input file:

how\'dy 'hello world' aaa\
bbb

The items in this file are how'dy, hello world and aaa␤bbb (where is a newline character).

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.