Reputation: 2187
How to refer the argument piped in?
For example, if you have a code test.sh
as below
#!/bin/bash
echo $1
when you run ./test.sh abcd.txt
, you will get abcd.txt printed on screen.
But when I try ls *.txt | ./test.sh
, I got all echoes empty lines.
Upvotes: 2
Views: 293
Reputation: 2868
you could try using xargs
:
find . -type f -name \*.txt -printf "%p\0" | xargs -0 -I xxx ./test.sh xxx
Upvotes: 3
Reputation: 2932
Data "piped in" is STDIN, but those are not command-line arguments. Here's an example printing each line of STDIN:
#!/usr/bin/env bash
while read line; do
echo "Line: $line"
done
There are many options to the Bash read
command. Read the SHELL BUILTIN COMMANDS
section of the man page for more info.
Upvotes: 1