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
Discover thousands of collaborative articles on 2500+ skills
Will court reporters hire proofreaders?
Hometown Pizza Sheridan Menu
Toa Guide Osrs
Research Tome Neltharus
Ixl Elmoreco.com
Chase Bank Operating Hours
Craigslist Kennewick Pasco Richland
Teamexpress Login
Remnant Graveyard Elf
Obituary Times Herald Record
Craigslist Greenville Craigslist
R/Altfeet
Nonuclub
Teenleaks Discord
Echat Fr Review Pc Retailer In Qatar Prestige Pc Providers – Alpha Marine Group
NBA 2k23 MyTEAM guide: Every Trophy Case Agenda for all 30 teams
Msu 247 Football
Timeforce Choctaw
Www.craigslist.com Savannah Ga
Gazette Obituary Colorado Springs
Talk To Me Showtimes Near Marcus Valley Grand Cinema
Project Reeducation Gamcore
Haunted Mansion Showtimes Near Epic Theatres Of West Volusia
48 Oz Equals How Many Quarts
Craigslist Panama City Beach Fl Pets
Tinyzonehd
Keshi with Mac Ayres and Starfall (Rescheduled from 11/1/2024) (POSTPONED) Tickets Thu, Nov 1, 2029 8:00 pm at Pechanga Arena - San Diego in San Diego, CA
Winterset Rants And Raves
Golden Tickets
Unity Webgl Player Drift Hunters
Vanessa West Tripod Jeffrey Dahmer
Quake Awakening Fragments
Crazy Balls 3D Racing . Online Games . BrightestGames.com
Legit Ticket Sites - Seatgeek vs Stubhub [Fees, Customer Service, Security]
Fetus Munchers 1 & 2
Restored Republic June 6 2023
Panorama Charter Portal
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Atom Tickets – Buy Movie Tickets, Invite Friends, Skip Lines
Acts 16 Nkjv
Sofia With An F Mugshot
Nu Carnival Scenes
Child care centers take steps to avoid COVID-19 shutdowns; some require masks for kids
Paperlessemployee/Dollartree
Dragon Ball Super Card Game Announces Next Set: Realm Of The Gods
The Quiet Girl Showtimes Near Landmark Plaza Frontenac
Germany’s intensely private and immensely wealthy Reimann family
Craigslist Anc Ak
211475039
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.