Shell – How to read from a pipe keeping positional parameters

argumentspipeshell-script

I'm writing a small script that I'd like to make it working from a piped input or else from the command-line arguments.

my_function() {
  for i in "$@"; do
    echo "$i"
    echo -----
  done
}

if [ -t 0 ]; then
  my_function "$@"    # command-line arguments
else
  my_function $(cat)  # pipe
fi

These options work:

$ my_script 1 2 3 4
$ echo 1 2 3 4 | my_script
1
-----
2
-----
3
-----
4
-----

This also works fine:

$ my_script 1 "2 3" 4
1
-----
2 3
-----
4
-----

The problem comes when I'm trying to keep positional parameters from a pipe:

$ echo 1 "2 3" 4 | my_script
1
-----
2
-----
3
-----
4
-----

$ echo 1 "'2 3'" 4 | my_script
1
-----
'2
-----
3'
-----
4
-----

Instead of my_function $(cat), I've tried to use read without good results, something like:

read input
my_function $input

Any ideas on this? I know word splitting is hard.

Best Answer

Have a look in this technique with bash 4.4:

function myf { 
  if ! [ -t 0 ];then 
    declare -a args=\($(</dev/stdin)\);
    set -- "${args[@]}"; 
  fi;
  for i in "$@";do 
    echo "$i";
    echo -----;
  done 
}

Testing:

$ function myf { if ! [ -t 0 ];then declare -a args=\($(</dev/stdin)\);set -- "${args[@]}"; fi;for i in "$@";do echo "$i";echo -----;done }
$ myf 1 "2 3" 4
1
-----
2 3
-----
4
-----

$ echo $'1 "2 3" 4'|myf
1
-----
2 3
-----
4
-----

Same result can be achieved also with:

$ echo 1 \"2 3\" 4 |myf
$ echo 1 "'2 3'" 4 |myf

But echo 1 "2 3" 4 is not going to work because double quotes are ignored by bash in this syntax:

$ echo 1 "2 3" 4
1 2 3 4