Passing Command Line Arguments to Bash Scripts (2024)

passing command line arguments to bash scripts

A command line argument is a parameter that we can supply to our Bash script at execution. They allow a user to dynamically affect the actions your script will perform or the output it will generate.

To pass an argument to your Bash script, your just need to write it after the name of your script:

./script.sh my_argument

In our Bash script, there are several reserved/pre-defined variables which we can use to recall the user-defined parameters. The first argument is stored in $1, the second in $2, the third in $3…and so on. We cannot use $0 as that references your Bash script itself.

Let’s see how this works using an example script:

If we run our script and don’t give an argument, we will see no output for our pre-defined variables:

./script.sh
The first fruit is:
The second fruit is:
The third fruit is:

Alternatively, if we provide three fruits, our script detects these and will return those values back to use via the pre-defined variables.

./fruit.sh apple pear orange
The first fruit is: apple
The second fruit is: pear
The third fruit is: orange

Sometimes, we may want to access all of the arguments. We can do this using $@.

Let’s update our example script to return all of the fruits provided as arguments to the script as well:

#!/usr/bin/env bash

echo "The first fruit is: $1"
echo "The second fruit is: $2"
echo "The third fruit is: $3"
echo "All fruits are: $@"

When we run our script, you can see that we now have an extra output which lists all of the fruits we gave to our script on the command line.

./fruit.sh apple pear orange
The first fruit is: apple
The second fruit is: pear
The third fruit is: orange
All fruits are: apple pear orange

© Wellcome Genome Campus Advanced Courses and Scientific Conferences

This article is from the online course:

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Passing Command Line Arguments to Bash Scripts (2)

Created by

Passing Command Line Arguments to Bash Scripts (3)

This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Passing Command Line Arguments to Bash Scripts (4)

Created by

Passing Command Line Arguments to Bash Scripts (5)

Passing Command Line Arguments to Bash Scripts (6)

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now

Register to receive updates

  • Create an account to receive our newsletter, course recommendations and promotions.

    Register for free
Passing Command Line Arguments to Bash Scripts (2024)
Top Articles
Should You Offer Free Shipping To Boost Sales? Here’s Why & How?
3 moving average crossover strategy
Mickey Moniak Walk Up Song
How To Fix Epson Printer Error Code 0x9e
Directions To Franklin Mills Mall
Hk Jockey Club Result
10 Popular Hair Growth Products Made With Dermatologist-Approved Ingredients to Shop at Amazon
Mylaheychart Login
Free VIN Decoder Online | Decode any VIN
Ashlyn Peaks Bio
Embassy Suites Wisconsin Dells
Paketshops | PAKET.net
Katie Boyle Dancer Biography
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Corporate Homepage | Publix Super Markets
LA Times Studios Partners With ABC News on Randall Emmett Doc Amid #Scandoval Controversy
Craigslist Chautauqua Ny
Belly Dump Trailers For Sale On Craigslist
Guidewheel lands $9M Series A-1 for SaaS that boosts manufacturing and trims carbon emissions | TechCrunch
180 Best Persuasive Essay Topics Ideas For Students in 2024
Scenes from Paradise: Where to Visit Filming Locations Around the World - Paradise
Delaware Skip The Games
Culver's Flavor Of The Day Taylor Dr
Orange Pill 44 291
Pecos Valley Sunland Park Menu
Xfinity Outage Map Fredericksburg Va
Play It Again Sports Norman Photos
Riversweeps Admin Login
All Obituaries | Verkuilen-Van Deurzen Family Funeral Home | Little Chute WI funeral home and cremation
Turbo Tenant Renter Login
1145 Barnett Drive
Effingham Daily News Police Report
Fuse Box Diagram Honda Accord (2013-2017)
Obsidian Guard's Skullsplitter
6465319333
O'reilly Auto Parts Ozark Distribution Center Stockton Photos
T&J Agnes Theaters
Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
Ticket To Paradise Showtimes Near Regal Citrus Park
Daly City Building Division
Qlima© Petroleumofen Elektronischer Laserofen SRE 9046 TC mit 4,7 KW CO2 Wächter • EUR 425,95
Nba Props Covers
Luvsquad-Links
Dr Mayy Deadrick Paradise Valley
Craigslist Woodward
Marcal Paper Products - Nassau Paper Company Ltd. -
Skyward Cahokia
De boeken van Val McDermid op volgorde
Smoke From Street Outlaws Net Worth
Edt National Board
Yoshidakins
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 5726

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.