How to Write Bash Script to Accept Input Parameters

How to Write Bash Script to Accept Input Parameters thumbnail
Learn to control your machine by leveraging the power of Bash.

Bash is the default shell for Unix. If you're not familiar with what a "shell" is, it's alternatively known as a "command prompt" and "terminal" on Windows and Mac systems, respectively. The shell provides a text-only method of interacting with the operating system and its utilities. Bash is also scriptable, meaning that you can automate it to carry out some repetitive tasks. Adding command line hooks, or input parameters, is crucial to being able to vary the behavior of your scripts. Fortunately, Bash makes this easy for us.

Instructions

    • 1

      Open the Bash script you would like to accept input parameters. This can be done by opening your favorite text editor.

    • 2

      Decide what command line handles you would like your script to respond to. Commands take the form <command> <input parameters .... >, where each of the parameters is separated by a space.

    • 3

      In your script, use the parameters as necessary. They can be accessed using the global environment variables $1, $2 and so forth for each respective handle. In plain English, when a user enters a command, Bash automatically captures the input parameters and hands them to us to use in conveniently defined variables.

Related Searches:

References

  • Photo Credit Jupiterimages/Goodshoot/Getty Images

Comments

You May Also Like

Related Ads

Featured