Shell Scripting Tutorial for Beginners 4 - Pass Arguments to a Bash-Script

preview_player
Показать описание
Passing arguments to a shell script

Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.

The following parameters are recognized:

$*
Returns a single string (``$1, $2 ... $n'') comprising all of the positional parameters separated by the internal field separator character (defined by the IFS environment variable).

$@
Returns a sequence of strings (``$1'', ``$2'', ... ``$n'') wherein each positional parameter remains separate from the others.

$1, $2 ... $n
Refers to a numbered argument to the script, where n is the position of the argument on the command line. In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.

$0
Refers to the name of the script itself.

$#
Refers to the number of arguments specified on a command line.

★★★Top Online Courses From ProgrammingKnowledge ★★★

★★★ Online Courses to learn ★★★

★★★ Follow ★★★

DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!
Рекомендации по теме
Комментарии
Автор

Good idea to show two screens, one with the editing and the other with output at the same time. Not many tutorials bother doing that.

marklambert
Автор

One of the best tutorials on Shell Scripting out there. Thanks a lot man, Appreciate your effort.

mamtachahal
Автор

i have seen alot of videos and your are the most educational. you are the first person who video i saw after i went from w2k to Linux. you showed me how to load things. thank you!

charlesklein
Автор

best videos on shell scripting seen till date. Thanks a ton!!

ShwetaSingh-ugtk
Автор

This whole series is amazing. Thank you so much!. Viewing this in 2021!

orestisiona
Автор

well, you have made shell scripting as easy as possible.

pmrm
Автор

Hello
can you please explain in details why we need the Pass Arguments to a Bash-Script ?
please reply me as soon as possible.
And Thanks for wonderful tutorials for shell scripting language

fatehsinghsunny
Автор

I know its very late, but one thing, at the end when the cards to some other videos and your channel's appear it blocks almost all the screen. 8:25

mamtachahal
Автор

Hi sir, you expalained all videos nicely but have one doubt that all these videos are enough for 2+ exp or not?

surajkalyankar
Автор

Best explanation and it's helpful for the students like me whole series is outstanding thanks man ! and you also having nice voice .

renishdevani
Автор

it helped me in my script - array input as stdin. Thankyou for sharing this.

shwetasingh-xjlq
Автор

Hi
First of all good tutorial.
I've tried myself the examples in in this video but I've noticed a different behaviour.
When assigning to a variable the array of input parameters like you do with
args=("$@")
it seems to refer to all parameters except the script name
I've tried my script calling ./myscript.sh one two three
when it reached the statement
echo "Parameters from input: ${args[1]}, ${args[2]}"
I expected it to print "one, two". Instead it printed "two, three"
Modifing the statement like
echo "Parameters from input: ${args[0]}, ${args[1]}"
the result was what I expected
Also, statement
echo $@
seemed to print input parameters except script name.
How do you explain this?
Hallo and thanks from Italy

Cerlienkus
Автор

good tutorials
one request - please explain how array (or similar tool/program functionality) works in details before using them in examples

tip for beginners: if ur scratching ur head wondering why shell didnt print actual values but only variable names when he used
echo 'echo > $0 $1 $2 $3'
its because shell doesnt expand variables in single quotes (but it does in double quotes - see prev examples)

tusharghude
Автор

Need help from you man. It could save my job!! How do I create a shell script and run it in a remote system in-memory without actually creating the script file using ssh.

I cannot use scp

ronaldmcsidy
Автор

is " " really required in passing values/arguments/messages in the echo statement ?

venuvenu
Автор

Sir. ..one doubt..
cd command is not working when it is included in the bash script..
How to solve this problem 🤔🤔

vaishnav
Автор

sir can you please give us your source code it will more help us . to solving the problems

geniusworld
Автор

Make some apache videos later please too. Generically.

Автор

Dude im broke af but i would definitely contribute if you started a gofundme for these kind of videos

dustybasement
Автор

Why did you put '> echo $1 $2 S3' ?
Even without that, it's displaying the result.

shalvi