lolibility
lolibility

Reputation: 2187

bash refer the argument/variable piped in

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

Answers (2)

Jay jargot
Jay jargot

Reputation: 2868

you could try using xargs:

find . -type f -name \*.txt -printf "%p\0" | xargs -0 -I xxx ./test.sh xxx 

Upvotes: 3

Eric Bolinger
Eric Bolinger

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

Related Questions