How To Pass and Parse Linux Bash Script Arguments and Parameters

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/how-to-pass-and-parse-linux-bash-script-arguments-and-parameters/
PINTEREST
PINTEREST
INSTAGRAM

Bash is powerful scripting language provides by various Linux distributions,Unix and BSD. If we need to make our script dynamic we generally use arguments. Arguments are provided to the script through command line. In this tutorial we will examine different use cases of argument passing and examples.

Syntax

In order to use externally provided values inside bash script we should provide them after the script name. Following syntax assumes the script is executable.

Or we can use bash in order to interpret our script like below and provide parameters.

Provide Arguments

We have learned the syntax of the arguments. In this example we will look use cases about argument passing. We will provide two arguments for example.

If we need provide string with multiple words we can use single or double quotes. In this example we will prove 10 and Hello Poftut as two separate argument.

Read Argument Inside Script

Now the real work starts now. We have learned how to provide arguments from shell or command line. In this part we will look how to read them inside script. Bash provides $1 , $2 ,  … like usage for reading input arguments inside script. the first argument can be accessed from variable name $1 , second one $2 and so …

In this example we will provide two argument Hello and Poftut to script.

LEARN MORE  Bash For and While Loop Examples

Read Argument Inside Script

Read Argument Inside Script

As we can see provided arguments are printed with echo

Assign Provided Arguments To Bash Variable

Another use case for bash variables is assigning them new variables with meaningful names. This will give us ability to change provided values and use them properly with their names. In this example we will put Hello and Poftut into variables named salute and name .

Assign Provided Arguments To Bash Variable

Assign Provided Arguments To Bash Variable

Read Multiple Arguments with For or While

Some times some arguments will not fit our condition and we may need a lot of argument to provide and use them in a iterative way. In this condition we can use bash loop mechanisms for and while . We can iterate over given arguments like and array or list with for and while . In this example we will iterate over provided arguments and print them to the shell. We will use $@ to specify the list of provided arguments and put each item into variable named var in each step.

Read Multiple Arguments with For or While

Read Multiple Arguments with For or While

Read With Parameter Names

As regular Linux applications we can use parameter names for arguments and parse them accordingly. We will use getops function which will parse provided options in an array. In this example we will use while loop with case structure. We will parse the provided parameter names u which is username and p password.

LEARN MORE  Linux Tee Command Tutorial with Examples For Stdout Management

Read With Parameter Names

Read With Parameter Names

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/how-to-pass-and-parse-linux-bash-script-arguments-and-parameters/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)