Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass args for script when going thru pipe

Tags:

bash

I have this example shell script:

echo /$1/ 

So I may call

$ . ./script 5 # output: /5/ 

I want to pipe the script into sh(ell), but can I pass the arg too ?

cat script | sh  # output: // 
like image 319
PeterMmm Avatar asked Feb 04 '13 18:02

PeterMmm


People also ask

How do you pass arguments in a script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

Can we use pipe in shell script?

The pipe character | is used to connect the output from one command to the input of another. > is used to redirect standard output to a file. Try it in the shell-lesson-data/exercise-data/proteins directory!

Can you pass in an argument to a bash script?

You can pass the arguments to any bash script when it is executed. There are several simple and useful ways to pass arguments in a bash script. In this article guide, we will let you know about some very easy ways to pass and use arguments in your bash scripts.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

You can pass arguments to the shell using the -s option:

cat script | bash -s 5 
like image 114
Anton Kovalenko Avatar answered Sep 17 '22 22:09

Anton Kovalenko


Use bash -s -- <args>

e.g, install google cloud sdk

~ curl https://sdk.cloud.google.com | bash -s -- --disable-prompts

like image 23
northtree Avatar answered Sep 18 '22 22:09

northtree