Recent Posts

ads

Bash Script 4 - Argument






Argument




Please remember this:
$0 is the filename of our script
$1 is the script argument 
$# the number of arguments

We will try to run our script called script.sh






This is our script in nano



Now let's add something else in our text editor to the script



Now let's see it in our terminal

Now it's not the name of our file script, but the first argument  as a result


Now let's add a couple random names

We can also add different variables to this script



Try to remember this:

If some_program were a bash shell script, we could read each item on the command line because the positional parameters contain the following:
  • $0 would contain "name of our program"
  • $1 would contain "word1"
  • $2 would contain "word2"
  • $3 would contain "word3"

So let's apply to our text editor, and check this first




Now let's add $2 to our bash script and see what happens






We have some issue with using double digits



The solution for this is brace brackets



To count the number of arguments:



The result is this



If we add another variable, this happens




To see all the arguments:


This is the result


This information is going to be very useful in your next bash script blog.



If it was useful, please click like and share! Thank you in advance!
Bash Script 4 - Argument Bash Script 4 - Argument Reviewed by ohhhvictor on May 27, 2019 Rating: 5

No comments:

Facebook

ads
Powered by Blogger.