Learn About: bash script command line arguments – bash Scripting, bash arg script arguments, bash args, bash command tutorial, how to bash script?, ubuntu shell script tutorial and bash argv shell argument
Command Line Arguments in Bash script
You can easy way to pass command line arguments to bash shell script. These are useful to create a script more user-friendly dynamic. Learn All about the Bash command arguments below Details.
You can also my best article to free download centos
.
Syntax:
$ ./myscript.sh ARG1 ARG2
You can with use special types bash arg, bash argv of the variables provided by the terminal, in your bash script code.
bash script arguments example
here, Command line input arguments can be passed just after script your file name with space separated. If any argument have with space, update them under provides single or double quote. Read below bash-scripting simple script.
#!/bin/bash ### Print total input arguments with their varible values echo "Total Arguments:" $# echo "All Arguments values:" $@ ### Command arguments can be get as echo "First->" $1 echo "Second->" $2 # You can also get the all arguments in an array as well as use them varible in a script. args=("$@") echo "First->" ${args[0]} echo "Second->" ${args[1]}
Last step to run or execute this base script with Two input arguments read with get the following outputs.
$ ./arguments.sh Hello PakaInfo
Results:
root@pakainfo : ~# root@pakainfo : ~# root@pakainfo : ~# ./arguments.sh Hello PakaInfo Total Arguments : 2 All Arguments values : Hello PakaInfo First-> Hello Second->PakaInfo First-> Hello Second->PakaInfo root@pakainfo : ~# root@pakainfo : ~#
Bash scripts are generally used to perform a variety of run time input tasks. These bellow examples information differents ways you can work with CMD to your get results command line input arguments.
Pass arguments through to another program
Bash scripts are always used as wrappers to launch second fresh application. A common task is to pass the input command line arguments from the shell Based script to the Example being loaded. Here is best way to perform demo of passing all the commandline arguments supported as-is.
#!/bin/bash # print_args.sh echo "You supported the arguments:" "$@" # You could pass all commandline arguments to second Example like this # myProgram "$@"
Get the number of arguments passed
The number of commandline arguments passed is saved or included in the $# variable.
#!/bin/bash echo "You provided $# arguments"
Accessing a specific argument by index
You can get a specific argument by its index like this:
#!/bin/bash echo "parameters 0: $0" echo "parameters 1: $1" echo "parameters 2: $2"
Base Argument 0 is the name of the script accept arguments being invoked itself.
Iterating through each argument
This example displays how to repeat through each read argument one-by-one, as well as print out the shell value.
#!/bin/bash for parameters in "$@" do echo "$parameters" done
Check arguments for specific value
If you wanted to repeat through each argument as well as check or read if one equals a particular data value, you can do that simple base code with the following Example:
#!/bin/bash for parameters in "$@" do if [ "$parameters" == "--help" ] || [ "$parameters" == "-h" ] then echo "Useful Example of the argument detected." fi done
Ensure the bash args script has the executable or run this example get the permission, as well as run it with a some pass commandline arguments to bash script.
Web Programming Tutorials Example with Demo
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about bash script commandline arguments using bash Scripting.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.