Shell - Pass output as argument to next command

发布时间 2023-12-28 13:13:01作者: ZhangZhihuiAAA

 

In bash we can pass the output of one command to the next one as an argument. We will cover multiple examples.

xargs is very useful for passing information between commands when chaining is used in Bash:

echo -e "Python\nJava" | xargs -I{} echo 'test me in' {}

result:

test me in Python
test me in Java

Another example of passing output as argument with cURL:

cat urls.txt | xargs -I % curl http://example.com/%.json

 

zzh@ZZHPC:/zdata/Github/zimplebank$ ls -1 db/migration/*.down.sql | xargs -I{} ls -l {}
-rw-rw-r-- 1 zzh zzh 160 Dec 27 19:36 db/migration/000000_init_schema.down.sql
-rw-rw-r-- 1 zzh zzh 40 Dec 27 19:36 db/migration/000001_add_sessions.down.sql
-rw-rw-r-- 1 zzh zzh 105 Dec 27 19:37 db/migration/000002_add_verification_emails.down.sql
zzh@ZZHPC:/zdata/Github/zimplebank$ ls -1 db/migration/*.down.sql | xargs -I % ls -l %
-rw-rw-r-- 1 zzh zzh 160 Dec 27 19:36 db/migration/000000_init_schema.down.sql
-rw-rw-r-- 1 zzh zzh 40 Dec 27 19:36 db/migration/000001_add_sessions.down.sql
-rw-rw-r-- 1 zzh zzh 105 Dec 27 19:37 db/migration/000002_add_verification_emails.down.sql