Skip to main content
removed comment and now obsolete (mispelled) attribution
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

I'm not entirely sure I understand your question, but I think what you are looking for is:

script.sh "$some_multiline_string" "$another_multiline_string" param1 param2

Then, inside the script, you would have:

file1="$1"
file2="$2"
param1="$3"
param2="$4"

If you really need to pipe it, you could do something like this:

printf '%s\0%s' "$str1" "$str2" | script.sh param1 param2

And, in the script:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
while IFS= read -d '' str; do
    strings+=("$str")
done
printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

For example:

$ str1="this is
a multiline
string"

$ str2="this is
another multiline
string"

$ printf '%s\0%s\0' "$str1" "$str2" | foo.sh
String 1: this is
a multiline
string

String 2: this is
another multiline
string

As @StéphaneChazellas pointed out int the comments, inIn bash versions 4.4+, you can do:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
readarray -t -d '' strings

printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

        

I'm not entirely sure I understand your question, but I think what you are looking for is:

script.sh "$some_multiline_string" "$another_multiline_string" param1 param2

Then, inside the script, you would have:

file1="$1"
file2="$2"
param1="$3"
param2="$4"

If you really need to pipe it, you could do something like this:

printf '%s\0%s' "$str1" "$str2" | script.sh param1 param2

And, in the script:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
while IFS= read -d '' str; do
    strings+=("$str")
done
printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

For example:

$ str1="this is
a multiline
string"

$ str2="this is
another multiline
string"

$ printf '%s\0%s\0' "$str1" "$str2" | foo.sh
String 1: this is
a multiline
string

String 2: this is
another multiline
string

As @StéphaneChazellas pointed out int the comments, in bash versions 4.4+, you can do:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
readarray -t -d '' strings

printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

        

I'm not entirely sure I understand your question, but I think what you are looking for is:

script.sh "$some_multiline_string" "$another_multiline_string" param1 param2

Then, inside the script, you would have:

file1="$1"
file2="$2"
param1="$3"
param2="$4"

If you really need to pipe it, you could do something like this:

printf '%s\0%s' "$str1" "$str2" | script.sh param1 param2

And, in the script:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
while IFS= read -d '' str; do
    strings+=("$str")
done
printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

For example:

$ str1="this is
a multiline
string"

$ str2="this is
another multiline
string"

$ printf '%s\0%s\0' "$str1" "$str2" | foo.sh
String 1: this is
a multiline
string

String 2: this is
another multiline
string

In bash versions 4.4+, you can do:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
readarray -t -d '' strings

printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

        
added 298 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

I'm not entirely sure I understand your question, but I think what you are looking for is:

script.sh "$some_multiline_string" "$another_multiline_string" param1 param2

Then, inside the script, you would have:

file1="$1"
file2="$2"
param1="$3"
param2="$4"

If you really need to pipe it, you could do something like this:

printf '%s\0%s' "$str1" "$str2" | script.sh param1 param2

And, in the script:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
while IFS= read -d '' str; do
    strings+=("$str")
done
printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

For example:

$ str1="this is
a multiline
string"

$ str2="this is
another multiline
string"

$ printf '%s\0%s\0' "$str1" "$str2" | foo.sh
String 1: this is
a multiline
string

String 2: this is
another multiline
string

As @StéphaneChazellas pointed out int the comments, in bash versions 4.4+, you can do:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
readarray -t -d '' strings

printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

        

I'm not entirely sure I understand your question, but I think what you are looking for is:

script.sh "$some_multiline_string" "$another_multiline_string" param1 param2

Then, inside the script, you would have:

file1="$1"
file2="$2"
param1="$3"
param2="$4"

If you really need to pipe it, you could do something like this:

printf '%s\0%s' "$str1" "$str2" | script.sh param1 param2

And, in the script:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
while IFS= read -d '' str; do
    strings+=("$str")
done
printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

For example:

$ str1="this is
a multiline
string"

$ str2="this is
another multiline
string"

$ printf '%s\0%s\0' "$str1" "$str2" | foo.sh
String 1: this is
a multiline
string

String 2: this is
another multiline
string

I'm not entirely sure I understand your question, but I think what you are looking for is:

script.sh "$some_multiline_string" "$another_multiline_string" param1 param2

Then, inside the script, you would have:

file1="$1"
file2="$2"
param1="$3"
param2="$4"

If you really need to pipe it, you could do something like this:

printf '%s\0%s' "$str1" "$str2" | script.sh param1 param2

And, in the script:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
while IFS= read -d '' str; do
    strings+=("$str")
done
printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

For example:

$ str1="this is
a multiline
string"

$ str2="this is
another multiline
string"

$ printf '%s\0%s\0' "$str1" "$str2" | foo.sh
String 1: this is
a multiline
string

String 2: this is
another multiline
string

As @StéphaneChazellas pointed out int the comments, in bash versions 4.4+, you can do:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
readarray -t -d '' strings

printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

        
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

I'm not entirely sure I understand your question, but I think what you are looking for is:

script.sh "$some_multiline_string" "$another_multiline_string" param1 param2

Then, inside the script, you would have:

file1="$1"
file2="$2"
param1="$3"
param2="$4"

If you really need to pipe it, you could do something like this:

printf '%s\0%s' "$str1" "$str2" | script.sh param1 param2

And, in the script:

#!/bin/bash
param1="$1"
param2="$2"

strings=()
while IFS= read -d '' str; do
    strings+=("$str")
done
printf 'String 1: %s\n\nString 2: %s\n' "${strings[0]}" "${strings[1]}"

For example:

$ str1="this is
a multiline
string"

$ str2="this is
another multiline
string"

$ printf '%s\0%s\0' "$str1" "$str2" | foo.sh
String 1: this is
a multiline
string

String 2: this is
another multiline
string